1

Here is a list

l1 = list(apple=c(2,23,43), pear = c(4,5), pineapple= 2, banana=97)

and here is the data.frame I am trying to construct from this list.

data.frame(fruit = c("apple","apple","apple","pear","pear","pineapple", "banana"), number = c(2,23,43,4,5,2,97))
      fruit number
1     apple      2
2     apple     23
3     apple     43
4      pear      4
5      pear      5
6 pineapple      2
7    banana     97

Can you help me with that?

Remi.b
  • 17,389
  • 28
  • 87
  • 168

1 Answers1

2

You can try melt

library(reshape2)
setNames(melt(l1), c('number', 'fruit'))

or a base R option is stack

stack(l1)
akrun
  • 874,273
  • 37
  • 540
  • 662