I'm trying to simultaneously merge several data frames in r by combining them into lists and using mapply and merge. This works fine when merging on a single key, but not when merging on multiple keys using the 'by = c("a","b")' argument in merge.
Mapply appears to be treating the by argument as a third list to cycle through, rather than passing the whole argument through every time.
For instance this code:
df <- data.frame(a = rep(1:3, 3),
b = c(rep("aa",3), rep("bb",3), rep("cc",3)),
c = rnorm(9, mean = 5))
df2 <- df1 <- df
list1 <- list(df, df1, df2)
df3 <- data.frame(a = rep(3:1, 3),
b = c(rep("cc",3), rep("bb",3), rep("aa",3)),
c = rnorm(9, mean = -5))
df5 <- df4 <- df3
list2 <- list(df3, df4, df5)
list3 <- mapply(merge, list1, list2, SIMPLIFY = FALSE, by = c("a","b"))
returns the warning message "longer argument not a multiple of length of shorter". But when I add a third term 'by = c("a","b","c")' it attempts to merge the first data frame by "a", the second by "b", and the third by "c". What I want, however, is to merge them all by both a and b. Does anyone know how I might do this?