I am trying to create a custom function that I can apply across various columns to recode values from characters to numeric data. The data has many blanks and each character entry is the same in each given column (ie. when there is a survey question that is "select all that apply" so you need to create binary 1/0 variables if the choice was selected). So logical i am trying to create a function that does the following:
"In a specified range of columns in the data, if there is any character present, recode that entry as 1, otherwise mark as NA"
This works as a standalone function as follows perfectly:
data$var <- if_else(data$var == data$var[grep("[a-z]", data$var)], 1, NULL)
But I am having trouble creating a function that does this that I can apply to many different columns.
I have tried to solve this with lapply, mutate, and if_else in the following ways to no avail.
I can return the indices correctly with the following fxn but need to update the actual dataframe:
fxn <- function(x) {
if_else(x == (x[grep("[a-z]", x)]), 1, NULL)
}
fxn(data$variable)
But when I try to use mutate to update the dataframe as follows it doesn't work:
data %>%
mutate(across(.cols = variable, fxn))
Any help would be appreciated as there are 100 columns I need to do this on!
CodePudding user response:
We create the function and apply to the selected columns with lapply
. In the below example, columns 1 to 5 are selected and applied the function, and assigned back
fxn <- function(x) NA^(!grepl('[a-z]', x))
data[1:5] <- lapply(data[1:5], fxn)