gpt4 book ai didi

r - 嵌套列表到单个表(按列名合并?)

转载 作者:行者123 更新时间:2023-12-04 04:57:25 25 4
gpt4 key购买 nike

我有一个嵌套列表,如下所示:

dput(head(tempList))
structure(list(Species = c("RandArcBo1", "RandArcBo1", "RandArcBo1",
"RandArcBo1", "RandArcBo1", "RandArcBo1", "RandArcBo1", "RandArcBo1",
"RandArcBo1", "RandArcBo1", "RandArcBo1", "RandArcBo1", "RandArcBo1",
"RandArcBo1", "RandArcBo1", "RandArcBo1"), x = c(132.5, 156.5,
178.5, 159.5, 166.5, 133.5, 103.5, 162.5, 165.5, 143.5, -163.5,
178.5, 151.5, 163.5, -120.5, -151.5), y = c(84.567321777, 83.567321777,
60.567321777, 77.567321777, 56.567321777, 74.567321777, 85.567321777,
70.567321777, 55.567321777, 74.567321777, 66.567321777, 72.567321777,
81.567321777, 53.567321777, 85.567321777, 76.567321777), Species = c("RandArcBo2",
"RandArcBo2", "RandArcBo2", "RandArcBo2", "RandArcBo2", "RandArcBo2",
"RandArcBo2", "RandArcBo2", "RandArcBo2", "RandArcBo2", "RandArcBo2",
"RandArcBo2", "RandArcBo2", "RandArcBo2", "RandArcBo2", "RandArcBo2"
), x = c(150.5, 121.5, 169.5, 174.5, 175.5, 153.5, -97.5, 169.5,
159.5, 166.5, -114.5, -92.5, -176.5, -167.5, 136.5, -133.5),
y = c(55.567321777, 76.567321777, 58.567321777, 80.567321777,
83.567321777, 82.567321777, 83.567321777, 57.567321777, 75.567321777,
55.567321777, 74.567321777, 77.567321777, 68.567321777, 67.567321777,
74.567321777, 70.567321777)), .Names = c("Species", "x",
"y", "Species", "x", "y"))

我想要的是将这些数据连接到具有三列“物种”、“x”和“y”的单个表中(每行都标有可能重复的物种名称,后跟 x,y 坐标)。 Cbind 将第二个顺序列表放在彼此相邻的列中,所以我最终得到的列数等于 3* 一阶对象的数量。 Rbind 将第二个订单列表放入行中,这样我最终得到的列数等于第二个订单列表的长度。有什么建议吗?

最佳答案

这是一个通用的方法:

data.frame(sapply(unique(names(tempList)), 
function(name) do.call(c, tempList[names(tempList) == name]), simplify=FALSE)
)

关于r - 嵌套列表到单个表(按列名合并?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16594981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com