Match and replace multiple lines in a text vector without looping in R

I am trying to apply gsub in R to replace the match in line a with the corresponding match in line b. For instance:

a <- c("don't", "i'm", "he'd")
b <- c("do not", "i am", "he would")
c <- c("i'm going to the party", "he'd go too")
newc <- gsub(a, b, c)

so newc = "I'm going to the party", "he will go too") This approach does not work, since gsub only accepts a string of length 1 for a and b. Running a loop for a loop through a and b will be very slow, since the real a and b have a length of 90 and c have a length of> 200,000. Are there vectors in R to perform this operation?

+5
source share
2 answers

1) gsubfn gsubfn gsubfn gsub, , , , -. , .

library(gsubfn)
gsubfn("\\S+", setNames(as.list(b), a), c)

:

[1] "i am going to the party" "he would go too"    

2) gsub :

cc <- c
for(i in seq_along(a)) cc <- gsub(a[i], b[i], cc, fixed = TRUE)

:

> cc
[1] "i am going to the party" "he would go too"        
+12

stringr::str_replace_all() - :

library(stringr)
names(b) <- a
str_replace_all(c, b)
[1] "i am going to the party" "he would go too"  

, , :

to_replace <- a
replace_with <- b
target_text <- c

names(replace_with) <- to_replace
str_replace_all(target_text, replace_with)
0

Source: https://habr.com/ru/post/1673115/


All Articles