'How to filter with a exact string?

I need to filter a column called "text" in this database called "Amazon_2020". When I use the code script below it filters words that contain part of the letters. For example, the word "acknowledgment" it is getting filtering words with -ment, too.

Dt <- Amazon_2020 %>% filter(grepl(pattern = "ableism|acceptance|access|acknowledge|acknowledging|acknowledgment",Amazon_2020$text))

How can I get exactly the words?

r


Solution 1:[1]

It is a case of partial matching. Here, we need to use word boundary (\\b) to match the whole words

library(dplyr)
Amazon_2020 %>%     
       filter(grepl(pattern = "\\b(ableism|acceptance|access|acknowledge|acknowledging|acknowledgment)\\b",
  text))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun