The issue is that c()
doesn't play nicely with tidy evaluation so we just have to find another way to deliver the joining condition.
Since the by
argument of the *_join
functions actually takes a named character vector as the input, we can achieve this with setNames
(or even set_names
from purrr
if you want to stay within the tidyverse).
library(dplyr)
DF1 <- data.frame(i = rep("T1", 5),
j = c("T1", "T10", "T11", "T12", "T2"),
var = c(291058, 8297, 3889, 17064, 12163), stringsAsFactors = FALSE)
DF2 <- data.frame(CODE = "T1", X = 2.34, Y = 48.86, stringsAsFactors = FALSE)
my_function <- function(tab, pt, origin, id) {
tab %>%
left_join(pt, by = setNames(id, nm = origin)) %>%
rename(Xi = X, Yi = Y)
}
my_function(DF1, DF2, origin = "i", id = "CODE")
i j var Xi Yi
1 T1 T1 291058 2.34 48.86
2 T1 T10 8297 2.34 48.86
3 T1 T11 3889 2.34 48.86
4 T1 T12 17064 2.34 48.86
5 T1 T2 12163 2.34 48.86
Note: When using setNames
or set_names
, the order of the arguments is inverted from the way we normally pass them to by
. So the y table's column needs to be specified first and the x table's second (as the argument to nm
).