I am trying to "center" multiple columns in a dataframe using dplyr but I keep getting a "non-numeric argument to binary operator" evaluation error. I think it is because I am trying to pass a string in when my function expects a bare variable name. However, using the syms() function does not help.
center <- function(var) {
var <- enquo(var)
var_ctrd <- paste0(quo_name(var), "_ctrd")
dataset <- dataset %>%
group_by(Gender) %>%
mutate(!! var_ctrd := !! var - mean(!! var, na.rm = TRUE))
}
# Pull out character vector of modifier names
mod_names <- dataset %>%
select(NeckLengthCm:FlexExtDiff_Peak_abs) %>%
colnames()
# Iterate over modifiers
walk(syms(mod_names), center)
Does anyone know how to solve this or if there is a better solution?