将`list`转换为`data.frame`而不会丢失名称

时间:2015-01-26 17:50:04

标签: r list dataframe

这是一个清单

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

这是我试图从这个列表构建的data.frame

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

你能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

您可以尝试melt

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

base R选项为stack

stack(l1)