【问题标题】:Function to create list of list from two data.frames从两个data.frames创建列表列表的函数
【发布时间】:2017-01-23 04:29:36
【问题描述】:

我有两个 data.frames firstdf 和 secondf,(下面的示例数据。)我正在尝试创建一个函数,它可以输出类似于下面的 ExampleList 数据的结果。我想创建一个列表列表,它从 firstdf 中获取第一行条目并将它们的值放入 exampleList 中的事物和测试字段中,然后从 seconddf 中的 otherthing 字段中获取前 3 个值,将它们连接在一起,并保存它们在 exampleList 的 otherthing 字段中,然后移动到 firstdf 中的下一行和 seconddf 中的下 3 行。循环对我来说有点棘手,因此非常感谢您的提示。

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"

【问题讨论】:

标签: r loops lapply


【解决方案1】:

您可以使用Map,这是lapply 的多变量版本(split 代表otherthing)。第一个参数是一个应用于多个参数的函数,这些参数将被并行迭代,所以

ExampleList <- Map(list, 
    thing = as.character(firstdf$thing), 
    test = as.character(firstdf$test), 
    otherthing = split(as.character(seconddf[[1]]), rep(1:3, each = 3)))

str(ExampleList)

## List of 3
##  $ thing1:List of 3
##   ..$ thing     : chr "thing1"
##   ..$ test      : chr "test1"
##   ..$ otherthing: chr [1:3] "thing4" "thing5" "thing6"
##  $ thing2:List of 3
##   ..$ thing     : chr "thing2"
##   ..$ test      : chr "test2"
##   ..$ otherthing: chr [1:3] "thing7" "thing8" "thing9"
##  $ thing3:List of 3
##   ..$ thing     : chr "thing3"
##   ..$ test      : chr "test3"
##   ..$ otherthing: chr [1:3] "thing10" "thing11" "thing12"

【讨论】:

  • 谢谢,您的建议非常适合我的示例数据。但是,我的真实数据 secondddf 数据要长得多,当我尝试您的建议时出现以下错误。有没有办法修改你的代码来处理更长的 seconddf?错误:警告消息:在 split.default(as.character(seconddf[[1]]), rep(length(firstdf$thing), 中:数据长度不是拆分变量的倍数
  • 查看?split。从示例中我假设您希望将其分成三部分,但如果您的实际数据不同,您需要构建一个要拆分的向量,该向量的长度与要拆分的向量相同,每个拆分都有一个唯一值我想结束。
猜你喜欢
  • 2020-02-23
  • 2020-06-09
  • 2017-05-08
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多