Masi Masi - 22 days ago 8
R Question

How to create Minimal List of two Lists in R?

I am trying to create a minimal list of two lists in R based on the thread How to make a great R reproducible example? but it does not cover the list of lists as a basic example.
Wanted minimum data sample of the following structure where B length can be minimised but must be heterogeneous

List of 2
$ :'data.frame': 541650 obs. of 2 variables:
..$ V1: num [1:541650] -0.21 -0.205 -0.225 -0.22 -0.21 -0.19 -0.205 -0.205 -0.205 -0.205 ...
..$ V2: num [1:541650] -0.245 -0.22 -0.2 -0.2 -0.195 -0.2 -0.19 -0.185 -0.18 -0.185 ...
$ :'data.frame': 426098 obs. of 2 variables:
..$ V1: num [1:426098] -0.285 -0.285 -0.185 -0.285 -0.385 -0.305 -0.305 -0.125 -0.015 -0.285 ...
..$ V2: num [1:426098] -0.445 -0.6 -0.815 -0.665 -0.49 -0.68 -0.555 -0.755 -0.795 -0.405 ...


Doing
dput(files)
, you get too big example data.
Doing
reproduce(files)
, the same case.
I also tried
dput(head(files, 5))
but to one list and returns then only 5 digits.
Pseudocode


  • structure List A of two lists B(1,2) where the length of B(1,2) varies such that

    length(B(1)_i) = length(B(2)_i)

  • minimise length of B(1,2) but keep heterogeneous length of subsequent Bs



Wanted output structure

List of 2
$ data.frame 11 obs. of 2 variables
$ V1: num [1:11] -0.21 -0.205 ...
$ V2: num [1.11] -0.245 -0.22 ...
$ data.frame 7 obs. of 2 variables
$ V1: num [1:7] -0.285 -0.286 ...
$ V2: num [1.7] -0.445 -0.6 ...


R: 3.3.1

OS: Debian 8.5

Answer

This can be done in a couple of ways. Create the two data.frame and place it in a list

lst <- list(data.frame(V1 = rnorm(11), V2 = rnorm(11)), 
             data.frame(V1 = rnorm(7), V2 = rnorm(7))) 

Or if there are many data.frames to be create, use a loop

lst1 <- lapply(c(11, 7), function(n) data.frame(V1 = rnorm(n), 
                               V2 = rnorm(n)))
Comments