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 existing column in R

I have dataframe containing following two columns

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

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

     Tumor_Barcode   Sex   Sample_ID
     MEL-JWCI-WGS-1   Male  ME001
     MEL-JWCI-WGS-11   Male ME011
     MEL-JWCI-WGS-12 Female ME012
     MEL-JWCI-WGS-13   Male ME013

Is there anyway i can do it in R?

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-JWCI-WGS-1","MEL-JWCI-WGS-11","MEL-JWCI-WGS-12","MEL-JWCI-WGS-13")
Sex<-c("Male", "Male", "Female", "Male")
DF1<-data.frame(Tumor_Barcode,Sex)

>Solution :

A possible solution:

library(tidyverse)

DF1 %>% 
  mutate(Sample_ID = str_c("ME", str_extract(Tumor_Barcode, "\\d+$") %>% 
         str_pad(3, pad = "0")))

#>     Tumor_Barcode    Sex Sample_ID
#> 1  MEL-JWCI-WGS-1   Male     ME001
#> 2 MEL-JWCI-WGS-11   Male     ME011
#> 3 MEL-JWCI-WGS-12 Female     ME012
#> 4 MEL-JWCI-WGS-13   Male     ME013
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