Removing a group of words from a character vector

前端 未结 2 938
误落风尘
误落风尘 2020-12-10 15:43

Let\'s say that I have a character vector of random names. I also have another character vector with a number of car makes and I want to remove any occurrence of a car incid

相关标签:
2条回答
  • 2020-12-10 16:28
    gsub(x = dat, pattern = paste(car, collapse = "|"), replacement = "")
    [1] "Tony" "Dave" "Alex"
    
    0 讨论(0)
  • 2020-12-10 16:29

    Just formalizing 42-'s comment above. Rather than using

    car = c("Honda","Ford","Toyota","honda","ford","toyota")
    

    You can just use:

    carlist = c("Honda","Ford","Toyota")
    
    gsub(x = dat, pattern = paste(car, collapse = "|"), replacement = "", ignore.case = TRUE)
    [1] "Tony" "Dave" "Alex"
    

    That allows you to only put each word you want to exclude in the list one time.

    0 讨论(0)
提交回复
热议问题