I have ten age columns in my data frame named similarly (i.e. agehhm1, agehhm2, …, agehhm10) that should hold age in years for a person. Currently, they are all strings as some observations include the words "month", "mos", etc. as some people are less than 1 year old. I am trying to use lapply to loop through these columns and replace observations that include these string patterns with a "0" value. I am close but am getting stuck on how to name the new columns I want to assign the lapply output to. I am trying setNames. I am not getting an error, but nothing is changing in my dataframe.
I am trying the following. I store the 10 age columns in an object "hhages_varnames". Then I apply lapply to this list of objects, and replace the applicable obs in each one with 0 if I find any of the "month" text patterns. I am trying to create new columns named agehhm1_clean, etc as output.
I am open to any other methods that you think are better for any part of this.
hhages_varnames is just an object where I store the names of the 10 age columns. So it is just a 1:10 vector with "agehhm1" "agehhm2",..."agehhm10".
hhages_varnames <- ls(dataframe_name, pattern = "agehhm.*")
setNames(lapply(hhages_varnames, FUN = function(x) (replace(x, grepl("month|MO|mos|days|months", dataframe_name[,x]),"0"))),
paste(names(hhages_varnames),"clean", sep="_"))