I have the following data frame:
df <- data.frame("A" = c("y", "y", "n", "n"),
"B" = c("n", NA, "y", "y"),
"C" = c("n", "y", "y", "n"))
I would like to apply the following code to columns A and B:
df$A <-
df$A %>%
recode(
"n" = "No",
"y" = "Yes"
) %>%
factor(
levels = c("No", "Yes")
)
I tried to solve this using the following code using a for loop:
cols <- c("A", "B")
for (i in cols) {
df$i <-
df$i %>%
recode(
"n" = "No",
"y" = "Yes"
) %>%
factor(
levels = c("No", "Yes")
)
}
However, I get this erroe message:
Error in UseMethod("recode") :
no applicable method for 'recode' applied to an object of class "NULL"
Can anybody help me what I am missing here? Thanks for your help!