I'm trying to compose a function that cleans my variables, replacing "*" with "1" and NAs with "0". I can do this easily with a ifelse, but I wanted it to be clean and use functional programming, but I clearly am not there yet...
An example database is:
db <- data.frame(
name = c("Abel", "Abner", "Bianca", "Pedro", "Lucas"),
scholarship1 = c("*", "*", "*", "*", NA),
scholarship2 = c("*", NA, NA, "*", "*"))
)
My function is something like this:
Dichotomizer <- function(database, variable) {
variable <- enquo(variable)
database$variable <- ifelse(
is.na(database$variable),
0,
1
)
}
But it obviously doesn't work, and I can't find out why... I tried using eval and substitute, but it still incurs in errors.
I appreciate any inputs to solve my problem. Thanks.