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

Editing Data frames in R and formatting

I was wondering how I can edit my data frame. I have a data frame like the one below.

   ID    Value
  A1_5    5.6
  A2_7    3.4
  A3_8    6.7 

I want to duplicate the IDs by changing the first letter, and repeating the same value since they are coming from the same samples. So I want to create a data frame such as…

   ID    Value
  A1_5    5.6
  B1_5    5.6
  A2_7    3.4
  B2_7    3.4
  A3_8    6.7 
  B3_8    6.7 

How can I go about doing this?

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

Thank you in advance.

>Solution :

library(dplyr)
data %>%
    mutate(
        ID = stringr::str_replace(ID, 'A', 'B')
    ) %>%
    bind_rows(data)
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