Floris Floris - 1 year ago 96
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 |

Answer Source

We can do this with sub

names(df1) <- sub("_.*", "", names(df1))
df1$moment <- "mean"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download