1

I have several lists (ListA, ListB, ListC...) with the same internal structure as the example below. I would like to combine all of them, keeping their structure, and have one list with all lists (ListAll). How can I do this? Example:

I have:

ListA
$ data  :'data.frame':  1 obs. of  2 variables:
  ..$ mean: num -0.128
  ..$ sd    : num 1.11
 $ simulations  :'data.frame':  1000 obs. of  2 variables:
  ..$ mean: num [1:1000] -0.0116 -0.0156 0.0336 -0.0502 -0.0427 ...
  ..$ sd    : num [1:1000] 1.003 1.014 0.963 1.036 1.051 ...
 $ values:'data.frame': 35 obs. of  2 variables:
  ..$ C: num [1:35] 3.45 2.91 2.62 2.06 1.87 ...
  ..$ D: num [1:35] 5.42 2.89 3.34 1.68 1.43 ...
and several lists with the same structure.

I would like to get:

ListAll
$ ListA
 $ data  :'data.frame':  1 obs. of  2 variables:
  ..$ mean: num -0.128
  ..$ sd    : num 1.11
 $ simulations  :'data.frame':  1000 obs. of  2 variables:
  ..$ mean: num [1:1000] -0.0116 -0.0156 0.0336 -0.0502 -0.0427 ...
  ..$ sd    : num [1:1000] 1.003 1.014 0.963 1.036 1.051 ...
 $ values:'data.frame': 35 obs. of  2 variables:
  ..$ C: num [1:35] 3.45 2.91 2.62 2.06 1.87 ...
  ..$ D: num [1:35] 5.42 2.89 3.34 1.68 1.43 ...
$ ListB
 $ data  :'data.frame':  1 obs. of  2 variables:
  ..$ mean: num -0.132
  ..$ sd    : num 1.01
 $ simulations  :'data.frame':  1000 obs. of  2 variables:
  ..$ mean: num [1:1000] -0.0114 -0.0123 0.0378 -0.0102 -0.0340 ...
  ..$ sd    : num [1:1000] 1.013 1.011 0.876 1.012 1.023 ...
 $ values:'data.frame': 35 obs. of  2 variables:
  ..$ C: num [1:35] 4.41 1.61 1.42 1.96 2.07 ...
  ..$ D: num [1:35] 2.41 2.19 2.54 2.08 2.53 ...

** and names(listAll) would be:**

ListaA, ListB, ListC...
BMT
  • 65
  • 6
  • 1
    `lst` is handy as it keeps the name of the list `listAll <- tibble::lst(listA, listB, listC)` – user63230 Jan 12 '21 at 17:58
  • thanks, @user63230 it works. I was wondering how to do this without using ```tidyverse```. – BMT Jan 12 '21 at 18:22

1 Answers1

1

You can create a list of lists in base R.

ListAll <- list(ListA, ListB, ListC)
SteveM
  • 2,226
  • 3
  • 12
  • 16
  • SteveM using only ```list``` did not keep the names of internal lists and their elements. That way I can't call any part of the listAll easily. – BMT Jan 12 '21 at 18:20
  • 1
    Yeah, I see. So you would need to name the list elements: `ListAll <- list(ListA = ListA...)` or else apply the names with `names(ListAll) <- c("ListA", "ListB",...)` There is also an `rlist` package: https://www.rdocumentation.org/packages/rlist/versions/0.4.6.1 that provides list management functions supplementary to the tidyverse. – SteveM Jan 13 '21 at 00:21