0

I have a list of lists

list_of_lists <- list(a = 1, 
                      b = 2:3,
                      c = 4:6)

and I want to convert it into a "long format" data frame of two columns. Something like

> rbind(data.frame(KEY = names(list_of_lists)[1], VALUE = list_of_lists[[1]]),
        data.frame(KEY = names(list_of_lists)[2], VALUE = list_of_lists[[2]]),
        data.frame(KEY = names(list_of_lists)[3], VALUE = list_of_lists[[3]]))

  KEY VALUE
1   a     1
2   b     2
3   b     3
4   c     4
5   c     5
6   c     6

I would know how to solve this problem with loops, but there must be a more direct way to do this. I tried to repurpose this question but lack enough understanding the tailor the answer to my question.
Bonus, if there is a dplyr-style solution to this.

chickenNinja123
  • 261
  • 2
  • 10

0 Answers0