Oscar
Oscar

Reputation: 51

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   |

Upvotes: 0

Views: 28

Answers (2)

akrun
akrun

Reputation: 887901

We can do this with sub

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

Upvotes: 1

StatMan
StatMan

Reputation: 636

I would do something like this:

names <- colnames(df)
for (i in 1:length(names))
{
  names[i] <- gsub("_mean", "", names[i])
}
colnames(df) <- names

Instead of the for loop you can also use one of the apply functions

Upvotes: 0

Related Questions