LivingstoneM
LivingstoneM

Reputation: 1088

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.

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.

Upvotes: 3

Views: 103

Answers (2)

akrun
akrun

Reputation: 887108

Using base R

data$Name_logic <-  sub('[preliminary_arm_1]', '', data$Name_logic, fixed = TRUE)

-output

> data
  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'

Upvotes: 2

TarJae
TarJae

Reputation: 78927

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'

Upvotes: 2

Related Questions