I have a few date-stamped files in a list of dataframes that I need to merge by date. Columns in the unmerged dataframes have inconsistent row numbers due to presence/absence of data for given dates, so the merge will produce null values which is OK. I'd like to define my own function which defines all of the date columns and then uses lubridate merge function, but I'm not sure what's going wrong.
library(lubridate)
X <- data.frame(Date = c("2000-01-01", "2000-02-01", "2000-04-01"), Values1 = c(1,2,4))
Y <- data.frame(Date = c("2000-01-01", "2000-04-01"), Values2 = c(1,4))
Z <- data.frame(Date = c("2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01"), Values3 = c(1,2,3,4))
merge.all <- function(x, by = "Date") {
for (i in seq_along(tables)) {
names(tables[[i]])[1] <- "Date"
names(tables[[i]])[2] <- "Values"
tables[[i]]$Date <- ymd(tables[[i]]$Date)
x <- merge(x, tables[[i]], by = by)
}
return(x)
}
merge.all(tables)