Search code examples
rreplacelapplynaming

R-naming the column output from lapply and replace


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="_")) 

Solution

  • UPDATE: Here is the final code that I got to do what I was wanted. It worked to use as.data.frame to make the vectors into a data frame. I also ended up using cbind to add the new columns to my existing dataframe. Thank you!

    hhages_varnames <- ls(dataframe_name, pattern = "agehhm.*")
    dataframe_name <- cbind(dataframe_name, setNames(as.data.frame(lapply(hhages_varnames, FUN = function(x) (replace(dataframe_name[,x], grepl("month|MO|mo|days|months", dataframe_name[,x]),"0")))), 
           paste0(as.list(hhages_varnames),"clean")))