Hello. I am a relative newcomer to R and I'm having some difficulty with what I thought would be a fairly simple function. Here is a sample:
Recode01 <- function(data, varname) {
varname = enquo(varname)
data <- data %>%
mutate((!!varname) :=
case_when((!!varname) == 1 ~ 4,
(!!varname) == 2 ~ 3,
(!!varname) == 3 ~ 1,
(!!varname) == 4 ~ 0,
(!!varname) == 5 ~ 2))
}
col1 <- c(1:5)
df1 <- as.data.frame(col1)
Recode01(df1, col1)
When I submit this literally nothing happens. Please note that when submitted as straight code it works as intended. See:
df1 <- df1 %>%
mutate(col1 =
case_when(col1 == 1 ~ 4,
col1 == 2 ~ 3,
col1 == 3 ~ 1,
col1 == 4 ~ 0,
col1 == 5 ~ 2))
Thank you in advance.