0

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

user33484
  • 740
  • 2
  • 9
  • 36
  • Does this answer your question? [Convert list of named vectors of different length to tibble](https://stackoverflow.com/questions/69840608/convert-list-of-named-vectors-of-different-length-to-tibble) – Karthik S Nov 04 '21 at 16:15

2 Answers2

2

You can use

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

stack(x)
Leonardo
  • 2,439
  • 33
  • 17
  • 31
  • similar `stack` question was asked just few hours ago https://stackoverflow.com/questions/69840608/convert-list-of-named-vectors-of-different-length-to-tibble – Karthik S Nov 04 '21 at 16:15
1

Using enframe

library(tibble)
library(tidyr)
enframe(x, name = 'l1', value = 'l2') %>% 
    unnest(l2)
# A tibble: 5 × 2
  l1    l2   
  <chr> <chr>
1 test1 a    
2 test1 b    
3 test1 c    
4 test2 x    
5 test2 y    
akrun
  • 874,273
  • 37
  • 540
  • 662