How can I recode a column using a function that decides which name an old variable name is to be changed to, based on a passed argument?
library(sjstats) # has example data
library(tidyverse)
data(efc)
# Function to decide the name of the variable
decide_name <- function(c161sex) {
name <- case_when(
c161sex == 1 ~ "Male",
c161sex == 2 ~ "Female",
TRUE ~ "missing"
)
return(name)
}
Recode the variable dynamically depending on the returned value (each dataset will have 1 and only 1 value for the variable "c161sex")
efc_renamed <- efc %>%
rename(decide_name = e42dep)