Home > database >  How do I transform list of lists into a dataframe where outerlists are columns and innerlist (of eac
How do I transform list of lists into a dataframe where outerlists are columns and innerlist (of eac

Time:08-04

I have a list of list with thousands of measurements. What I want is to transform the list of list into a dataframe where each outerlist is a column and the values of each innerlist are rows. So that list[[1]][1]] and list[[2]][[1]] are "rbinded" into the same column.

Example list:

set.seed(1)
list <- list(list(round(runif(2), 1), round(runif(2), 1), round(runif(2), 1)),
     list(round(runif(2), 1), round(runif(2), 1), round(runif(2), 1)),
     list(round(runif(2), 1), round(runif(2), 1), round(runif(2), 1)))

What I want is to transform it to the following dataset:

df <- data.frame(col1 = c(0.3, 0.4, 0.9, 0.7, 0.7, 0.4),
                col2 = c(0.6, 0.9, 0.6, 0.1, 0.8, 0.5),
                col3 = c(0.2, 0.9, 0.2, 0.2, 0.7, 1))

Is there a way to this automatically? Answers in baseR are very much appreciated.

Thanks for any help.

CodePudding user response:

A possible solution in base R:

do.call("rbind", lapply(list, \(x) setNames(data.frame(x),
  paste0("col", 1:length(x)))))

#>   col1 col2 col3
#> 1  0.3  0.6  0.2
#> 2  0.4  0.9  0.9
#> 3  0.9  0.6  0.2
#> 4  0.7  0.1  0.2
#> 5  0.7  0.8  0.7
#> 6  0.4  0.5  1.0

Another possible solution, using tidyverse:

library(tidyverse)

map_dfr(list, ~ data.frame(.x) %>% set_names(str_c("col", 1:length(.x))))

#>   col1 col2 col3
#> 1  0.3  0.6  0.2
#> 2  0.4  0.9  0.9
#> 3  0.9  0.6  0.2
#> 4  0.7  0.1  0.2
#> 5  0.7  0.8  0.7
#> 6  0.4  0.5  1.0

CodePudding user response:

Don't name your list list, in my code lst

sapply(
  1:length(lst),
  function(i){
    sapply(lst,"[[",i)
  }
)

     [,1] [,2] [,3]
[1,]  0.3  0.6  0.2
[2,]  0.4  0.9  0.9
[3,]  0.9  0.6  0.2
[4,]  0.7  0.1  0.2
[5,]  0.7  0.8  0.7
[6,]  0.4  0.5  1.0

CodePudding user response:

lapply(lst, data.table::transpose) |> 
  unlist() |> 
  matrix(ncol = lengths(lst)[[1]], byrow = TRUE)

     [,1] [,2] [,3]
[1,]  0.3  0.6  0.2
[2,]  0.4  0.9  0.9
[3,]  0.9  0.6  0.2
[4,]  0.7  0.1  0.2
[5,]  0.7  0.8  0.7
[6,]  0.4  0.5  1.0
  •  Tags:  
  • r
  • Related