Home > database >  Struggling to get str_replace to work in R
Struggling to get str_replace to work in R

Time:02-12

I'm struggling to get str_replace to work:

Data :

test <- structure(list(id_number = 101:106, why_join = c("Support for college success (examples: class selection, managing coursework, time management)", 
"Support for college success (examples: class selection, managing coursework, time management)", 
"Assistance with applying to (and enrolling in) college, Assistance with FASFA/financial aid, Support for college success (examples: class selection, managing coursework, time management), Identifying and enrolling in apprenticeship opportunities, Identifying and enrolling in certification programs", 
"Support for college success (examples: class selection, managing coursework, time management)", 
"Assistance with securing quality employment/career launch, Assistance with applying to (and enrolling in) college, Assistance with FASFA/financial aid, Support for college success (examples: class selection, managing coursework, time management), Identifying and enrolling in apprenticeship opportunities, Identifying and enrolling in certification programs, Interest in military service, Unsure/Exploratory", 
"Assistance with applying to (and enrolling in) college, Assistance with FASFA/financial aid, Support for college success (examples: class selection, managing coursework, time management), Identifying and enrolling in certification programs"
)), row.names = c(NA, 6L), class = "data.frame")

I'm trying to use str_replace to remove "(examples: class selection, managing coursework, time management)" from the strings "Support for college success (examples: class selection, managing coursework, time management)"

Code:

test$why_join <- str_replace(test$why_join, "Support for college success (examples: class selection, managing coursework, time management)", "Support for college success")

My output is not removing that part of the string. Where am I going wrong?

CodePudding user response:

By default, the str_replace is in regex mode. Wrap with fixed or else the (, ) are judged as metacharacters for capturing groups

library(stringr)
str_replace(test$why_join, 
   fixed("Support for college success (examples: class selection, managing coursework, time management)"), 
    "Support for college success")

-output

[1] "Support for college success"                                                                                                                                                                                                                                                                                                                          
[2] "Support for college success"                                                                                                                                                                                                                                                                                                                          
[3] "Assistance with applying to (and enrolling in) college, Assistance with FASFA/financial aid, Support for college success, Identifying and enrolling in apprenticeship opportunities, Identifying and enrolling in certification programs"                                                                                                             
[4] "Support for college success"                                                                                                                                                                                                                                                                                                                          
[5] "Assistance with securing quality employment/career launch, Assistance with applying to (and enrolling in) college, Assistance with FASFA/financial aid, Support for college success, Identifying and enrolling in apprenticeship opportunities, Identifying and enrolling in certification programs, Interest in military service, Unsure/Exploratory"
[6] "Assistance with applying to (and enrolling in) college, Assistance with FASFA/financial aid, Support for college success, Identifying and enrolling in certification programs"          
  •  Tags:  
  • r
  • Related