Here is an example list of lists of my real data:
df <- c(1, 2, 3)
x <- list(df, df, df)
y <- list(df, df, df)
z <- list(df, df, df)
lista <- list(x, y, z)
listb <- list(x, y, z)
lols <- list(a = lista, b =listb)
lols
has a structure like this:
lols
|
________
| |
a b
| |
_____ _____
| | | | | |
x y z x y z
I want to restructure lols
into the following shape:
lols
|
___________
| | |
x y z
| | |
___ ___ ___
| | | | | |
a b a b a b
I managed to do it using for
loops, but I am not sure if it is correct, and if it works efficiently with very large real data:
newl <- rep(list(list()), length(lols[[1]]))
for (i in seq_along(lols)) {
for(j in seq_along(lols[[i]])) {
newl[[j]][[i]] <- lols[[i]][[j]]
}
}
Is there a faster way to do it, as for
loop is believed very slow in R?
In my code the list names are dropped, how can I keep the names?
EDIT:
A microbenchmark based on mine and the accepted answer and purrr::transpose()
as suggested by the comment
fun1 <- function(ls) {
newl <- rep(list(list()), length(ls[[1]]))
for (i in seq_along(ls)) {
for (j in seq_along(ls[[i]])) {
newl[[j]][[i]] <- ls[[i]][[j]]
}
}
return(newl)
}
fun2 <- function(ls) {
nm <- el(lapply(ls, names))
newl <- lapply(nm, \(i) lapply(ls, '[[', i)) |> setNames(nm)
}
fun3 <- function(ls) {
purrr::transpose(ls)
}
microbenchmark::microbenchmark(fun1(loaded), fun2(loaded), fun3(loaded), times = 1000)
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> fun1(loaded) 7631.3 8029.35 8877.8146 8296.65 8946.65 37443.3 1000
#> fun2(loaded) 66.6 81.60 118.0540 113.75 135.00 923.9 1000
#> fun3(loaded) 2.9 3.90 16.0451 15.60 27.80 70.7 1000
CodePudding user response:
Better name your list elements, then you may loop over the lists' first layer names
.
nm <- el(lapply(lols, names))
newl <- lapply(nm, \(i) lapply(lols, '[[', i)) |> setNames(nm)
Gives
str(newl)
# List of 3
# $ x:List of 2
# ..$ a:List of 3
# .. ..$ k: num [1:3] 1 2 3
# .. ..$ l: num [1:3] 1 2 3
# .. ..$ m: num [1:3] 1 2 3
# ..$ b:List of 3
# .. ..$ k: num [1:3] 1 2 3
# .. ..$ l: num [1:3] 1 2 3
# .. ..$ m: num [1:3] 1 2 3
# $ y:List of 2
# ..$ a:List of 3
# .. ..$ k: num [1:3] 1 2 3
# .. ..$ l: num [1:3] 1 2 3
# .. ..$ m: num [1:3] 1 2 3
# ..$ b:List of 3
# .. ..$ k: num [1:3] 1 2 3
# .. ..$ l: num [1:3] 1 2 3
# .. ..$ m: num [1:3] 1 2 3
# $ z:List of 2
# ..$ a:List of 3
# .. ..$ k: num [1:3] 1 2 3
# .. ..$ l: num [1:3] 1 2 3
# .. ..$ m: num [1:3] 1 2 3
# ..$ b:List of 3
# .. ..$ k: num [1:3] 1 2 3
# .. ..$ l: num [1:3] 1 2 3
# .. ..$ m: num [1:3] 1 2 3
Or also without renaming, try
lapply(1:3, \(i) lapply(lols, '[[', i)) |> setNames(c('x', 'y', 'z'))
Data:
lols <- list(a = list(x = list(k = c(1, 2, 3), l = c(1, 2, 3), m = c(1,
2, 3)), y = list(k = c(1, 2, 3), l = c(1, 2, 3), m = c(1, 2,
3)), z = list(k = c(1, 2, 3), l = c(1, 2, 3), m = c(1, 2, 3))),
b = list(x = list(k = c(1, 2, 3), l = c(1, 2, 3), m = c(1,
2, 3)), y = list(k = c(1, 2, 3), l = c(1, 2, 3), m = c(1,
2, 3)), z = list(k = c(1, 2, 3), l = c(1, 2, 3), m = c(1,
2, 3))))