R – list to dataframe in certain format?

I have data structured like this (but with c. 1000 or so items):

x <- list("test1" = c("a","b","c"),
          "test2" = c("x","y"))

> x
$test1
[1] "a" "b" "c"

$test2
[1] "x" "y"

I’d like to structure it like this:

     l1 l2
1 test1  a
2 test1  b
3 test1  c
4 test2  x
5 test2  y

Ideally in a fairly fast/efficient way as my actual list is quite large

>Solution :

You can use

x <- list("test1" = c("a","b","c"),
          "test2" = c("x","y"))

stack(x)

Leave a Reply