3

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!

Veerendra Gadekar
  • 4,452
  • 19
  • 24
scrrd
  • 103
  • 1
  • 2
  • 7
  • You probably ought to make a reproducible example. My guess: do a single find-replace on `paste(garbage, collapse="|")` – Frank Feb 12 '16 at 15:56
  • Like [this](http://stackoverflow.com/a/35235271/4497050), but take out the regex and use `""` for all the replacement strings. – alistaire Feb 12 '16 at 16:05

1 Answers1

14

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" 
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197