I have a list of data.frames
. Each has the same two columns. I need to unlist it into a single data.frame. I have tried multiple things (including unlist
, converting to a matrix, etc. — including from some of the SO searches that come up first), but I am having trouble making it work (the unlist
command just generates a bunch of columns, while I need two columns and a bunch of rows).
I ended up writing a for
loop this instance, but it’s a problematic approach for well-known reasons.
Here is the data and my current approach:
> head(dotz, 2)
[[1]]
X1 X2
1 -83.06974 42.32464
2 -83.06155 42.32168
3 -83.06034 42.32120
4 -83.05915 42.32060
5 -83.05804 42.31984
6 -83.05711 42.31904
7 -83.05658 42.31851
8 -83.05592 42.31778
9 -83.05525 42.31695
10 -83.05458 42.31591
11 -83.05212 42.31150
12 -83.05171 42.31074
13 -83.05006 42.30802
[[2]]
X1 X2
1 -83.16668 42.31032
2 -83.16782 42.30987
> length(dotz)
[1] 3674
My approach:
buff <- dotz[[1]]
for (i in 2:length(dotz)) {
buff <- merge(buff, dotz[[i]], all = TRUE)
}
What would be the command that does the same, but more efficiently? Thanks. (I know, I could’ve pre-allocated the array size. In this case, it didn’t matter too much, and I am looking for a way that doesn’t involve a for
loop anyway.)