Manuel
Manuel

Reputation: 87

Change column in dataframe based on regex in R

I have a large dataframe with a column displaying different profiles:

PROFILE           NTHREADS TIME
profAsuffix       1        3.12
profAanother      2        1.9
profAyetanother   3 
...
profBsuffix       1        4.1
profBanother      1        3.9
...

I want to rename all profA* pattern combining them in one name (profA) and do the same with profB*. Until now, I do it as:

data$PROFILE <- as.factor(data$PROFILE)
levels(data$PROFILE)[levels(data$PROFILE)=="profAsuffix"] <- "profA"
levels(data$PROFILE)[levels(data$PROFILE)=="profAanother"] <- "profA"
levels(data$PROFILE)[levels(data$PROFILE)=="profAyetanother"] <- "profA"

And so on. But this time I have too many differents suffixes, so I wonder if I can use grepl or a similar approach to do the same thing.

Upvotes: 1

Views: 1639

Answers (1)

akrun
akrun

Reputation: 887118

We can use sub

data$PROFILE <- sub("^([a-z]+[A-B]).*", "\\1", data$PROFILE)

Upvotes: 3

Related Questions