2

我有一个看起来像这样的嵌套列表:

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 将二阶列表放入行中,这样我最终得到的列数等于二阶列表的长度。有什么建议么?

4

2 回答 2

3

这是一个通用的方法:

data.frame(sapply(unique(names(tempList)), 
     function(name) do.call(c, tempList[names(tempList) == name]), simplify=FALSE)
)
于 2013-05-16T18:56:57.743 回答
0

split对我来说unlist似乎是最直接的方法:

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
于 2013-12-19T19:13:05.150 回答