Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gsub for the list of multiple patterns in R [closed]

Tags:

r

This is kind of hard to explain, so please bear with me.

I would like to do a "find/replace" using a list of strings to "find" and an empty string ("") to replace. I have a large data table column I'd like to do this find/replace on.

Using base R, I can't figure out how to use a pattern list in gsub.

I have made a loop, but if someone could help me figure out how to use one of the apply functions (or something else in just base R), that would be MUCH more efficient and I would greatly appreciate it.

This works, but takes SO long:

for(i in 1:GarbMax){
      Table.All$Cleaned<-gsub(garbage[i], "", Table.All$Cleaned, ignore.case = TRUE, fixed = TRUE)
}

The list of values I'd like to find are in "garbage", the field I'm looking for them in is "Table.All$Cleaned". "GarbMax" is just the max value of the "garbage" list.

As an aside (maybe), the above code gives me a warning that ignore.case=TRUE is being ignored. Any idea why?

Thanks so much for your help!

like image 896
scrrd Avatar asked Dec 07 '25 08:12

scrrd


1 Answers

If I understand correctly, the following solution would be one way of going about:

string <- c("onetwo", "two", "three", "fourfive", "five", "six", "sixseven")
find.list <- list("two", "five", "seven")
# in REGEX, | also represents "OR"
find.string <- paste(unlist(find.list), collapse = "|")

gsub(find.string, replacement = "", x = string)
[1] "one"   ""      "three" "four"  ""      "six"   "six" 
like image 187
Roman Luštrik Avatar answered Dec 08 '25 22:12

Roman Luštrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!