I have a data frame and want to create a new variable c = b/a:
# 1. Using actual variable names - it works:
x <- data.frame(a = 1:5, b = 2:6)
x %>% mutate(c = b/a)
# 2. Using dynamic variable names - doesn't work:
x <- data.frame(a = 1:5, b = 2:6)
var_a <- "a"
var_b <- "b"
var_c <- "c"
x %>% mutate_(var_c = var_b/var_a)
x %>% mutate(!!var_c := (!!var_b) / (!!var_a))
What is wrong? I looked at the answers here (dplyr - mutate: use dynamic variable names) - but am still unclear how it should be done.
Thank you!