Floris Floris - 8 months ago 42
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:

|24 |25 |


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


We can do this with sub

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