Search code examples
rlistdataframeconcatenation

Transform List of List of Vectors into Dataframe in R


This is about transforming lists in R.

My data looks as follows:

>data = list(list(c('a', 'b'), c('c', 'd')), list(c('a', 'b', 'c'), c('d', 'e', 'f'), c('g', 'h', 'i')))
>data
[[1]]
[[1]][[1]]
[1] "a" "b"

[[1]][[2]]
[1] "c" "d"


[[2]]
[[2]][[1]]
[1] "a" "b" "c"

[[2]][[2]]
[1] "d" "e" "f"

[[2]][[3]]
[1] "g" "h" "i"

I would like to concatenate every vector of the sublists, e.g. paste("a", "b") = "a b"), so that the whole list can be transformed into a dataframe, that looks as follows:

> df <- data.frame(col1 = c("a b", "c d", NA), col2 = c("a b c", "d e f", "g h i"))
> df
  col1  col2
1  a b a b c
2  d e d e f
3 <NA> g h i

I tried to solve this problem now with the apply-functions in R, but this is really not my territory... Thx in advance for your help!


Solution

  • Here is something. We use two helper functions from data.table transpose() and setDF().

    library(data.table)
    nrs <- max(lengths(data))
    
    df <- setDF(
      lapply(lapply(data, transpose), function(x) do.call(paste, x)[1:nrs])
    )
    df
    #     V1    V2
    # 1  a b a b c
    # 2  c d d e f
    # 3 <NA> g h i
    

    EDIT:

    Dependency-free solution:

    as.data.frame(sapply(data, function(x) sapply(x, paste, collapse = " ")[1:nrs]))