I have the following piece of code that merges two dataframes:
prim <- data.frame("t"=2007:2012,
"a"=1:6,
"b"=7:12)
secnd <- data.frame("t"=2012:2013,
"a"=c(5, 7))
final_df <- prim %>% full_join(secnd, by = 't') %>%
mutate(a = coalesce(as.integer(a.y),a.x)) %>%
select(t,a,b)
Is it possible to use a variable name instead of hard-coding a
as done above? I.e., is it possible to make the following non-functioning code work?
var <- "a"
final_df <- prim %>% full_join(secnd, by = 't') %>%
mutate(var = coalesce(as.integer(var.y),var.x)) %>%
select(t,var,b)