I have a monstrous nested list structure of my own making that looks like this:
str(CMaster)
List of 4
$ :List of 6
..$ :List of 5
.. ..$ :List of 15
.. .. ..$ : num [1, 1:14] 0.144 0.2 0.256 0.352 0.446 ...
.. .. ..$ : num [1, 1:47] 0.144 0.2 0.375 0.54 0.694 ...
etc
$ :List of 6
..$ :List of 1
.. ..$ :List of 15
.. .. ..$ : num [1, 1:14] 0.144 0.2 0.256 0.352 0.446 ...
.. .. ..$ : num [1, 1:47] 0.144 0.2 0.375 0.54 0.694 ...
The structure is fixed but the last list of 15 could go up to 150K and I need to try to plot this structure. I'd like to try and plot boxplots categorised by the List of 4 variable for each of the List of 6 similar which condenses all of the numerical data for the List of 15 into this example. Do I need to unlist it all first? Is there an easier way to make a data.frame or data.table which preserves the names of the lists and makes them factors for plotting?
dfs <- lapply(CMaster, data.frame, stringsAsFactors = FALSE)
EDIT: I've added example code
Example code (that gets close to the real structure).
D<-list()
DNSIM<-list()
DTime<-list()
DMaster<-list()
for(CC in 1:4){
for(t in 1:6){
for(N in 1:5){
for(i in 1:15){
Dmatrix=runif(15)
D[[i]]=Dmatrix
}
DTime[[t]]=D
}
DNSIM[[N]]=DTime
}
DMaster[[CC]]=DTime
}
Dput
It's too big to copy and I my organisation won't allow a sharable link to onedrive. Any easy workaround?
EDIT2
tibble(lists = CMaster) %>%
+ mutate(CleaningType = row_number()) %>%
+ unnest_longer(lists, indices_to = "TimePoint") %>%
+ unnest_longer(lists, indices_to = "Replicate") %>%
+ unnest_longer(lists, indices_to = "BehaviourObservation")
# A tibble: 1,800 x 5
lists BehaviourObservation Replicate TimePoint CleaningType
<list> <int> <int> <int> <int>
1 <dbl[,14] [1 × 14]> 1 1 1 1
2 <dbl[,47] [1 × 47]> 2 1 1 1
3 <dbl[,11] [1 × 11]> 3 1 1 1
4 <dbl[,40] [1 × 40]> 4 1 1 1
5 <dbl[,40] [1 × 40]> 5 1 1 1
6 <dbl[,34] [1 × 34]> 6 1 1 1
7 <dbl[,92] [1 × 92]> 7 1 1 1
8 <dbl[,31] [1 × 31]> 8 1 1 1
9 <dbl[,5] [1 × 5]> 9 1 1 1
10 <dbl[,103] [1 × 103]> 10 1 1 1
# … with 1,790 more rows
So I tried to add another sub-sub-list and now get an error of incompatible sizes. Any thoughts about this please?
tibble(lists = CMaster) %>%
+ mutate(CleaningType = row_number()) %>%
+ unnest_longer(lists, indices_to = "TimePoint") %>%
+ unnest_longer(lists, indices_to = "Replicate") %>%
+ unnest_longer(lists, indices_to = "BehaviourObservation") %>%
+ unnest_longer(lists, indices_to = "sub_sub_observation")
Error: Can't combine `..1$lists` <double[,14]> and `..2$lists` <double[,47]>.
✖ Incompatible sizes 14 and 47 along axis 2.
Run `rlang::last_error()` to see where the error occurred.


