Home > database >  Split and remove part of a row value string in R
Split and remove part of a row value string in R

Time:01-29

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.

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.

CodePudding user response:

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'
  •  Tags:  
  • r
  • Related