[ACCEPTED]-Can I combine a list of similar dataframes into a single dataframe?-dataframe
do.call("rbind", foo)
should do the trick.
0
with plyr
:
foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)),
df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))
library(plyr)
ldply(foo)[,-1]
x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
0
There are several problems with your code.
The 11 first is that the assignment statement in 10 the list doesn't work. This needs to be 9 fixed by, for example:
foo <- list(
df1 = data.frame(x=c('a', 'b', 'c'), y = c(1,2,3)),
df2 = data.frame(x=c('d', 'e', 'f'), y = c(4,5,6))
)
You can then use rbind() to 8 combine the data frames:
rbind(foo$df1, foo$df2)
x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
But this poses more 7 questions. For example, why do you combine 6 the data frames in a list in the first place. The 5 second is whether you really need to use 4 data frames rather than vectors. Finally, I 3 generally try to avoid rbind() and rather 2 use merge() when combining data frames in 1 this way.
How about merge(foo[[1]], foo[[2]], all = TRUE)
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.