Replace a prefix of a string I have a data frame like this

&gt; head(a[1:2,1:4])
   Tumor_Sample_Barcode Chromosome Position End_Position
1:                   A1       chr4 90169866     90169866
2:                   A1      chr11 60235747     60235747
    &gt; dim(a)
[1] 753655    134
&gt;
&gt; unique(a$Tumor_Sample_Barcode)
 [1] "A1"  "A10" "A11" "A12" "A13" "A14" "A15" "A16" "A17" "A18" "A19" "A2"  "A20" "A21" "A22" "A23" "A24" "A25" "A26" "A27" "A28"
[22] "A29" "A3"  "A30" "A31" "A32" "A33" "A4"  "A5"  "A6"  "A7"  "A8"  "A9" 


I want to change a$Tumor_Sample_Barcode  from A1:A21 to res1:res21 but I don't how to do that
I think you are looking for gsub function:

a$Tumor_Sample_Barcode &lt;- gsub("A","res",a$Tumor_Sample_Barcode)


If you prefer, you can use the str_replace_all function from stringr package. But it tooks a little bit more time than gsub:

Dummy data

seq = as.factor(paste0("A",1:21))
library(data.table)
dt &lt;- data.table(Barcode = rep(seq,10000),
                 Value = rnorm(210000))


Test of gsub vs str_replace_all

&gt; system.time(dt$Barcode &lt;- gsub("A","res",dt$Barcode))
   user  system elapsed 
  0.077   0.000   0.077 

&gt; system.time(dt$Barcode &lt;- str_replace_all(dt$Barcode,"A","res"))
   user  system elapsed 
  0.123   0.004   0.126 
```