Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter rows that contain exact strings in a column

I need to filter rows from df that contain air as a stand-alone word in a column

library(dplyr)
df <- data.frame(x = c(13, 34, 5, 124, 56),
                 y = c('air transport', 'hairdressing', 'airport', 'repair', 'frontend'))

This gives me rows that partially contain air

df %>% filter(grepl('air',y))

    x             y
1  13 air transport
2  34  hairdressing
3   5       airport
4 124        repair

Here is my intended result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   x             y
1 13 air transport

>Solution :

Use \\b to match a zero-length word boundary:

df %>% 
  filter(grepl('\\bair\\b', y))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading