R - 将列表列表转换为单个数据帧

时间:2015-09-29 17:21:38

标签: r list dataframe

所以,我创建了一个包含256个嵌套列表的列表(和一个列矩阵)。我想做的是将256个列表中的每一个转换为16列的单个数据帧,然后write.table。尽管每个列表包含相同数量的列(16),但每个列表的行数会有所不同。我试图使用unlist失败,因为更改行数。我可以单独对每个列表进行子集化,因此我知道有更简单的方法来完成整个列表。

我对R很陌生,所以我为询问可能是一个天真的新手问题而道歉。我在过去几天搜索了很多主题,并没有看到任何与我的问题相符的内容。 for循环看起来似乎没必要,我也不确定lapply是否也是正确的路线。

第一个列表的

更新: dput

list(structure(list(structure(c(2L, 11L, 15L, 8L, 7L, 3L, 6L,  10L,
1L, 1L, 18L, 13L, 14L, 19L, 16L, 17L, 4L, 5L, 9L, 12L), .Label = c("",
"Aaron Rodgers", "Andrew Quarless", "Derrick Coleman", "Doug Baldwin",
"DuJuan Harris", "Eddie Lacy", "James Starks", "Jermaine Kearse", 
"John Kuhn", "Jordy Nelson", "Luke Willson", "Marshawn Lynch",  "Percy
Harvin", "Randall Cobb", "Ricardo Lockette", "Robert Turbin", 
"Russell Wilson", "Zach Miller"), class = "factor"), Tm =
structure(c(1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L, 3L, 3L, 3L, 3L,
3L, 3L, 3L,  3L, 3L, 3L), .Label = c("GNB", "Passing", "SEA", "Tm"),
class = "factor"), Cmp = structure(c(3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 
     2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "19", 
     "23", "Cmp", "Rushing"), class = "factor"), Att = structure(c(3L, 
     1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 4L, 2L, 1L, 1L, 1L, 1L, 1L, 
     1L, 1L, 1L, 1L), .Label = c("", "28", "33", "Att", "Receiving"
     ), class = "factor"), Yds = structure(c(2L, 1L, 1L, 1L, 1L, 
     1L, 1L, 1L, NA, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
     ), .Label = c("", "189", "191", "Yds"), class = "factor"), 
     TD = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 
     3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1", 
     "2", "TD"), class = "factor"), Int = structure(c(3L, 1L, 
     1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
     1L, 1L, 1L), .Label = c("", "0", "1", "Int"), class = "factor"), 
     Lng = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 4L, 
     3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "23", 
     "33", "Lng"), class = "factor"), Att = structure(c(1L, 1L, 
     1L, 7L, 3L, 1L, 2L, 2L, NA, 8L, 7L, 4L, 5L, 1L, 1L, 6L, 1L, 
     1L, 1L, 1L), .Label = c("", "1", "12", "20", "4", "6", "7", 
     "Att"), class = "factor"), Yds = structure(c(1L, 1L, 1L, 
     7L, 6L, 1L, 9L, 3L, NA, 10L, 5L, 2L, 8L, 1L, 1L, 4L, 1L, 
     1L, 1L, 1L), .Label = c("", "110", "2", "27", "29", "34", 
     "37", "41", "7", "Yds"), class = "factor"), TD = structure(c(1L, 
     1L, 1L, 2L, 2L, 1L, 2L, 3L, NA, 5L, 2L, 4L, 2L, 1L, 1L, 2L, 
     1L, 1L, 1L, 1L), .Label = c("", "0", "1", "2", "TD"), class = "factor"), 
     Lng = structure(c(1L, 1L, 1L, 2L, 4L, 1L, 8L, 6L, NA, 9L, 
     3L, 7L, 5L, 1L, 1L, 8L, 1L, 1L, 1L, 1L), .Label = c("", "12", 
     "13", "15", "16", "2", "21", "7", "Lng"), class = "factor"), 
     Rec = structure(c(1L, 7L, 5L, 3L, 4L, 4L, 1L, 1L, NA, 8L, 
     1L, 2L, 6L, 4L, 3L, 1L, 2L, 4L, 2L, 2L), .Label = c("", "1", 
     "2", "3", "6", "7", "9", "Rec"), class = "factor"), Yds = structure(c(1L, 
     12L, 9L, 3L, 3L, 6L, 1L, 1L, NA, 13L, 1L, 4L, 10L, 8L, 7L, 
     1L, 5L, 4L, 11L, 2L), .Label = c("", "1", "11", "14", "15", 
     "26", "38", "42", "58", "59", "8", "83", "Yds"), class = "factor"), 
     TD = structure(c(1L, 2L, 3L, 2L, 2L, 2L, 1L, 1L, NA, 4L, 
     1L, 2L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 2L), .Label = c("", "0", 
     "1", "TD"), class = "factor"), Lng = structure(c(1L, 7L, 
     9L, 3L, 4L, 8L, 1L, 1L, NA, 14L, 1L, 5L, 11L, 10L, 11L, 1L, 
     6L, 12L, 13L, 2L), .Label = c("", "1", "11", "12", "14", 
     "15", "16", "18", "23", "24", "33", "6", "8", "Lng"), class = "factor")), .Names = c("",  "Tm", "Cmp", "Att", "Yds", "TD", "Int",
 "Lng", "Att", "Yds",  "TD", "Lng", "Rec", "Yds", "TD", "Lng"),
 row.names = c(NA, -20L ), class = "data.frame"))

因此,我的列表中的每个观察都是如上所述,我想将所有列表转换为它们的16列(现在我想到它,它的17列,一个是未命名的)数据帧布局并将所有行堆叠在一起,然后我可以write.table

1 个答案:

答案 0 :(得分:2)

让我们拨打您的列表l[[1]],其中dput就是您上面的data.table

基础R和do.call("rbind", l) data.table::rbindlist(l)

的两种简单方法
1) {(name: 'fooman', email: 'foo@man.com')}
2) {(name: 'fooman', email: 'foo@man.com', adress:'fooadress')}
3) {(name: 'fooman', email: 'foo@man.com', hasdog:'true')}
4) {(name: 'fooman', email: 'foo@man.com', location: 'footown')}

这假设每个列表元素中的列匹配。你举例说明了你的例子并没有证实这一点。