【发布时间】:2016-05-09 14:21:11
【问题描述】:
在 R 中,我有一个 data.frame(或 data.table)。在这个data.frame中,我有一列,每个单元格都由一个列表列表(一个data.frame)组成。
我可以通过rbindlist(data$Subdocuments) 将此列转换为单个 data.frame,但这里缺少原始 data.frame 的其他列。
如何有效地解包这一列列表,但保持其他列附加到新的 data.frame?
library(data.table)
data <- structure(list(ID = c("1", "2", "3"), Country = c("Netherlands",
"Germany", "Belgium"), Subdocuments = list(structure(list(Value = c("5",
"5", "1", "3", "2", "1", "1", "1", "2", "5", "3", "2", "4", "5",
"5", "2"), Label = c("Test1", "Test2", "Test3", "Test4", "Test5",
"Test6", "Test7", "Test8", "Test9", "Test10", "Test11", "Test12",
"Test13", "Test14", "Test15", "Test16"), Year = c(2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
2014, 2015, 2016)), .Names = c("Value", "Label", "Year"), class = "data.frame", row.names = c(NA,
16L)), structure(list(Value = c("5", "4", "3", "2", "2", "2",
"1", "1", "5", "4", "4", "4", "5", "1", "1", "3"), Label = c("Test1",
"Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8",
"Test9", "Test10", "Test11", "Test12", "Test13", "Test14", "Test15",
"Test16"), Year = c(2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016)), .Names = c("Value",
"Label", "Year"), class = "data.frame", row.names = c(NA, 16L
)), structure(list(Value = c("1", "2", "3", "1", "1", "4", "5",
"1", "2", "3", "2", "2", "1", "1", "1", "5"), Label = c("Test1",
"Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8",
"Test9", "Test10", "Test11", "Test12", "Test13", "Test14", "Test15",
"Test16"), Year = c(2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016)), .Names = c("Value",
"Label", "Year"), class = "data.table", row.names = c(NA, 16L
)))), .Names = c("ID", "Country", "Subdocuments"), row.names = c(NA,
-3L), class = "data.frame")
【问题讨论】:
-
您的数据在
list列中显示了很多NA行。 -
@akrun 抱歉,data.frame 的输入有问题。我解决了。
-
也许是
setDT(data)[, .SD[[1L]][[1L]], by=.(ID, Country)]?
标签: r data.table