1

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.

Axeman
  • 32,068
  • 8
  • 81
  • 94
Manuel
  • 87
  • 1
  • 9
  • 1
    Possible duplicate of [Extracting the last n characters from a string in R](http://stackoverflow.com/questions/7963898/extracting-the-last-n-characters-from-a-string-in-r) – Sotos Jan 12 '17 at 11:04

1 Answers1

3

We can use sub

data$PROFILE <- sub("^([a-z]+[A-B]).*", "\\1", data$PROFILE)
akrun
  • 874,273
  • 37
  • 540
  • 662