0

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)

brbell01
  • 3
  • 1
  • Something like `Reduce(function(df1, df2) merge(df1, df2, by = "Date"), list(X, Y, Z))` in base R (corresponds to an inner join). Additional options are in the linked post. – Maurits Evers Apr 14 '22 at 05:03
  • The purrr reduce solution based on the linked post, worked like a charm. I had not come across this solution. Thank you! – brbell01 Apr 14 '22 at 22:02

0 Answers0