如果rbind.fill始终返回null,如何将列表放入数据框?

时间:2012-11-08 07:12:56

标签: r dataframe

我有这个清单:

myList<-list(c("Developmental Biology", " Neuroscience", " Cell Biology"
), c("Developmental Biology", " Neuroscience", " Cell Biology"
), c("Developmental Biology", " Neuroscience", " Genomics & Genetics"
), c("Developmental Biology", " Neuroscience", " Cell Biology"
), c("Developmental Biology", " Neuroscience", " Cell Biology"
), c("Developmental Biology", " Genomics & Genetics"))

我想要这个数据帧:

     [,1]                    [,2]                   [,3]                   
[1,] "Developmental Biology" " Neuroscience"        " Cell Biology"        
[2,] "Developmental Biology" " Neuroscience"        " Cell Biology"        
[3,] "Developmental Biology" " Neuroscience"        " Genomics & Genetics" 
[4,] "Developmental Biology" " Neuroscience"        " Cell Biology"        
[5,] "Developmental Biology" " Neuroscience"        " Cell Biology"        
[6,] "Developmental Biology" " Genomics & Genetics" "NA"

我原以为rbind.fill会成为可能,但do.call(rbind.fill,myList)会返回NULL

1 个答案:

答案 0 :(得分:3)

函数rbind.fill只能组合数据帧。但是列表myList中的元素是字符向量。在使用rbind.fill之前,需要将它们转换为数据框。

这就是诀窍:

library(plyr)
rbind.fill(lapply(myList, function(x) as.data.frame(t(as.matrix(x)))))

#                      V1                   V2                   V3
# 1 Developmental Biology         Neuroscience         Cell Biology
# 2 Developmental Biology         Neuroscience         Cell Biology
# 3 Developmental Biology         Neuroscience  Genomics & Genetics
# 4 Developmental Biology         Neuroscience         Cell Biology
# 5 Developmental Biology         Neuroscience         Cell Biology
# 6 Developmental Biology  Genomics & Genetics                 <NA>