Using the code Reduce
/ merge
from @akrun's answer will work fine if the values for the columns month
and are the year
same for each data frame. However, when they do not match (sample data at the end of this answer)
Reduce(function(...) merge(..., by = c('month', 'year')), ls)
will only return rows that are common to each data frame:
month year oracle microsoft google
1 3 2004 394.4286 357.7143 390.0000
2 4 2004 391.8571 347.1429 391.8571
all=TRUE
( @akrun) full_join
dplyr
, /:
library(dplyr)
Reduce(function(...) full_join(..., by = c('month', 'year')), ls)
# or just:
Reduce(full_join, ls)
:
month year oracle microsoft google
1 1 2004 356.0000 NA NA
2 2 2004 390.0000 339.0000 NA
3 3 2004 394.4286 357.7143 390.0000
4 4 2004 391.8571 347.1429 391.8571
5 5 2004 NA 333.2857 357.7143
6 6 2004 NA NA 333.2857
:
ls <- list(structure(list(month = 1:4, year = c(2004L, 2004L, 2004L, 2004L), oracle = c(356, 390, 394.4286, 391.8571)), .Names = c("month", "year", "oracle"), class = "data.frame", row.names = c(NA, -4L)),
structure(list(month = 2:5, year = c(2004L, 2004L, 2004L, 2004L), microsoft = c(339, 357.7143, 347.1429, 333.2857)), .Names = c("month", "year", "microsoft"), class = "data.frame", row.names = c(NA,-4L)),
structure(list(month = 3:6, year = c(2004L, 2004L, 2004L, 2004L), google = c(390, 391.8571, 357.7143, 333.2857)), .Names = c("month", "year", "google"), class = "data.frame", row.names = c(NA,-4L)))