我有两个data.frames firstdf和secondf,(示例数据如下。)我试图创建一个函数,可以输出结果,如下面的ExampleList数据。我想创建一个列表列表,它从firstdf获取第一行条目,并将它们的值放入exampleList中的thing和test字段中,然后从seconddf中的otherthing字段获取前3个值,并将它们连接在一起,并保存他们在exampleList中的其他字段中,然后移到firstdf中的下一行和seconddf中的下3个行中。循环对我来说有点棘手,所以提示是绝对赞赏的。函数来创建从两个data.frames列表的列表
data:
dput(firstdf)
structure(list(thing = structure(1:3, .Label = c("thing1", "thing2",
"thing3"), class = "factor"), test = structure(1:3, .Label = c("test1",
"test2", "test3"), class = "factor")), .Names = c("thing", "test"
), row.names = c(NA, -3L), class = "data.frame")
dput(seconddf)
structure(list(otherthing = structure(c(4L, 5L, 6L, 7L, 8L, 9L,
1L, 2L, 3L), .Label = c("thing10", "thing11", "thing12", "thing4",
"thing5", "thing6", "thing7", "thing8", "thing9"), class = "factor"),
other = structure(c(9L, 6L, 7L, 2L, 3L, 1L, 8L, 4L, 5L), .Label = c("fads",
"oiu", "qwer", "rewa", "rewq", "sfas", "sfwg", "tre", "xdfs"
), class = "factor")), .Names = c("otherthing", "other"), row.names = c(NA,
-9L), class = "data.frame")
输出:
dput(ExampleList)
list(structure(list(thing = "thing1", test = "test1", otherthing = c("thing4",
"thing5", "thing6")), .Names = c("thing", "test", "otherthing"
)), structure(list(thing = "thing2", test = "test2", otherthing = c("thing7",
"thing8", "thing9")), .Names = c("thing", "test", "otherthing"
)), structure(list(thing = "thing3", test = "test3", otherthing = c("thing10",
"thing11", "thing12")), .Names = c("thing", "test", "otherthing"
)))
[[1]]
[[1]]$thing
[1] "thing1"
[[1]]$test
[1] "test1"
[[1]]$otherthing
[1] "thing4" "thing5" "thing6"
[[2]]
[[2]]$thing
[1] "thing2"
[[2]]$test
[1] "test2"
[[2]]$otherthing
[1] "thing7" "thing8" "thing9"
[[3]]
[[3]]$thing
[1] "thing3"
[[3]]$test
[1] "test3"
[[3]]$otherthing
[1] "thing10" "thing11" "thing12"
的可能的复制http://stackoverflow.com/questions/26177565/converting-nested-list-to-dataframe – akrun
@akrun我愿意把这个作为重复,但我们需要一个更好的目标imho – Jaap
我说尽可能,而不是肯定 – akrun