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

Split and remove part of a row value string in R

Friends,

I have a data that looks like this :

data <- data.frame(
  SSN = c(204,401,101,666,777), 
  Name_logic=c("([preliminary_arm_1][antibiotic_arm] = '1') and [was_review_done] = '1'",
         "[preliminary_arm_1][antibiotic_arm]  = '2' and [was_review_done] = '1'",
         "[preliminary_arm_1][intervention_supportive_arm] = '2' and [was_review_done] = '1'",
         "[preliminary_arm_1][supportive_care_arm] = '1' and [was_there_an_sae] = '1'",
         "([preliminary_arm_1][antibiotic_arm] = '1') and [was_review_done] = '1'") 
  )

Now focusing on column Name_logic, I did like to remove the [preliminary_arm_1] part from every row value that it appears. I want to loop through every row and when it finds that part where there is [preliminary_arm_1] then remove it from the row value.

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

This is my expected output

data <- data.frame(
  SSN = c(204,401,101,666,777), 
  Name_logic=c("[antibiotic_arm] = '1' and [was_review_done] = '1'",
               "[antibiotic_arm]  = '2' and [was_review_done] = '1'",
               "[intervention_supportive_arm] = '2' and [was_review_done] = '1'",
               "[supportive_care_arm] = '1' and [was_there_an_sae] = '1'",
               "[antibiotic_arm] = '1' and [was_review_done] = '1'") 
)

Any help will be appreciated.

>Solution :

We could use str_remove from stringr package:

library(dplyr)
library(stringr)

data %>% 
  mutate(Name_logic = str_remove(Name_logic, "\\[preliminary_arm_1\\]"))
  SSN                                                      Name_logic
1 204            ([antibiotic_arm] = '1') and [was_review_done] = '1'
2 401             [antibiotic_arm]  = '2' and [was_review_done] = '1'
3 101 [intervention_supportive_arm] = '2' and [was_review_done] = '1'
4 666        [supportive_care_arm] = '1' and [was_there_an_sae] = '1'
5 777            ([antibiotic_arm] = '1') and [was_review_done] = '1'
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