Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recoding column data in R dataframe

I have dataframe containing following two columns

Tumor_Barcode    SEX
     MEL-1   Male
     MEL-11   Male
     MEL-12 Female
     MEL-13   Male

I want to recode column Tumor_Barcode and output should be as following.

Tumor_Barcode   SEX
     sample-1   Male  
     sample-11   Male 
     sample-12 Female 
     sample-13   Male 

Is there anyway i can do it in R? may be something with mutate function from dplyr or anything better?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Data:

Tumor_Barcode<-c(" MEL-1","MEL-11","MEL-12","MEL-13")
Sex<-c("Male", "Male", "Female", "Male")
DF1<-data.frame(Tumor_Barcode,Sex)

>Solution :

Using stringr

library(stringr)
df$Tumor_Barcode = str_replace(df$Tumor_Barcode, 'MEL', 'sample')

  Tumor_Barcode    Sex
1      sample-1   Male
2     sample-11   Male
3     sample-12 Female
4     sample-13   Male
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading