Floris Floris - 4 months ago 16
R Question

Trouble with removing part of string and creating variables in R

I have few variables with _mean in their name. I want to find these variables and remove _mean from their name whilst creating a variable moment = mean for them. I use the code below:

for (i in 1:length(files)){ #looping through files
dflist[[i]] <- read.dta13(files[i], nonint.factors = TRUE) #reading the files into R

if(grepl("_mean", colnames(dflist[[i]])) == TRUE){ #locating variables with _mean
dflist[[i]]$moment <- "mean" #creating moment=mean variable
str_replace(dflist[[i]], "\\_mean.*", "")} #removing _mean from string names
}


However, this gives me the following error:

1: In if (grepl("_mean", colnames(dflist[[i]])) == TRUE) { :
the condition has length > 1 and only the first element will be used


and no adjustments are made to the variable.

Output should look like:
Before

|variable1_mean|variable2_mean|
|24 |25 |


After

|moment| variable1 | variable2|
|mean | 24 | 25 |

Answer

We can do this with sub

names(df1) <- sub("_.*", "", names(df1))
df1$moment <- "mean"