Problem context: replace a chr variable in data frame by converting chr to numeric. Use a function for the chr to numeric, then apply() on passing in df_credit_status$length_of_service to be converted to numeric and to update that same data frame column, e.g., df_credit_status$length_of_service.
I have this function defined and the apply() function.
The data frame column field: df_credit_status$length_of_service is chr, for this update, I will need to possibly create a new field and then move / copy that result to df_credit_status$length_of_service, after changing the data type.
fun_year <- function(length) { as.numeric(gsub("[A-Za-z]|[[:punct:]]|\\s+", "", length))}
df_credit_status$length <- apply(df_credit_status$length_of_service, FUN=fun_year)
The error in apply() is dim(x) must have a positive length.
dput() has this data from df_credit_status$length_of_service
"< 1 year", "2 years", "3 years", "4 years", "5 years", "6 years", "7 years", "8 years", "9 years", "10+ years"
sapplyis probably what you wanted.applyis for objects that have a dimension (like a matrix)dim(x)method. A vector doesn't have that - dario