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

时间:2013-05-16 18:38:31

标签: r nested-lists rbind cbind

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

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"))

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

2 个答案:

答案 0 :(得分:3)

以下是一般方法:

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

答案 1 :(得分:0)

splitunlist对我来说似乎是最直截了当的方法:

out <- data.frame(lapply(split(tempList, names(tempList)), 
                         unlist, use.names = FALSE))
head(out)
#      Species     x        y
# 1 RandArcBo1 132.5 84.56732
# 2 RandArcBo1 156.5 83.56732
# 3 RandArcBo1 178.5 60.56732
# 4 RandArcBo1 159.5 77.56732
# 5 RandArcBo1 166.5 56.56732
# 6 RandArcBo1 133.5 74.56732
tail(out)
#       Species      x        y
# 27 RandArcBo2 -114.5 74.56732
# 28 RandArcBo2  -92.5 77.56732
# 29 RandArcBo2 -176.5 68.56732
# 30 RandArcBo2 -167.5 67.56732
# 31 RandArcBo2  136.5 74.56732
# 32 RandArcBo2 -133.5 70.56732