I want to write a function which joins two tibbles, with the 2nd tibble's joined column specified in the function's args.
I have
df1 <- tibble(NUMBER = c(1,4))
df2 <- tibble(ORDER = 1:5,
DISORDER = 5:1,
WORD = c("The", "quick", "brown", "fox", "jumped"))
I want
chozer(df1, df2, ORDER)
# to yield
tibble(NUMBER = c(1,4),
DISORDER = c(5,2),
WORD = c("The", "fox"))
# and
chozer(df1, df2, DISORDER)
# to yield
tibble(NUMBER = c(5,2),
DISORDER = c(1,4),
WORD = c("jumped", "quick"))
I've tried several variations on
chozer <- function(df1, df2, ColName){
aCol = enquo(ColName)
inner_join(df1, df2, by = c(NUMBER = aCol))
}
# but they all gave errors.
I looked thru Hadley Wickham's Advanced R, but found no examples of an NSE (nonstand evaluation) being used in the by
clause of a join.
Do you know how?