Question

I have a nested list that looks like this:

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

What I want is to join these data into single table with three columns, "Species", "x" and "y" (each row is labeled with a species name that may repeat, followed by an x,y coordinate). Cbind puts the second order lists in columns next to each other so I end up with a number of columns equal to 3* the number of first-order objects. Rbind puts the second order lists into rows so that I end up with a number of columns equal to the length of the second order lists. Any suggestions?

Was it helpful?

Solution

Here's a general approach:

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

OTHER TIPS

split and unlist seems to be the most straightforward approach to me:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top