I have a list of data frames that I would like to merge into a single data.frame file. Here is what my list looks like:
my_list <- list(
m=data.frame(a = letters[1:5], b = 1:5, c = rnorm(5)),
n=data.frame(a = letters[1:5], b = 6:10, c = rnorm(5)))
> my_list
$m
a b c
1 a 1 0.1151720
2 b 2 -0.3785748
3 c 3 -0.1446305
4 d 4 -0.4300272
5 e 5 1.1982312
$n
a b c
1 a 6 1.2079439
2 b 7 -1.2414251
3 c 8 0.4362390
4 d 9 -0.5844525
5 e 10 0.1420070
I would like to stack them on top of each other, but without losing the context of the name data.frame ("m", "n"). Ideally, the name of the source data frame will be included as an additional column in the final data frame. One way is to simply add an extra column before using rbind.fill:
for(i in 1:length(my_list)) my_list[[i]][, 4] <- names(my_list)[i]
library(plyr)
rbind.fill(my_list)
a b c V4
1 a 1 0.1151720 m
2 b 2 -0.3785748 m
3 c 3 -0.1446305 m
4 d 4 -0.4300272 m
5 e 5 1.1982312 m
6 a 6 1.2079439 n
7 b 7 -1.2414251 n
8 c 8 0.4362390 n
9 d 9 -0.5844525 n
10 e 10 0.1420070 n
What I don't like about this is that I have to take care of the size of the data frame and the name of the extra column.
Is there a feature that makes this better, more flexible, and more general?