Juliet R
Juliet R

Reputation: 223

How to filter data based on one word of a column value?

I have a dataframe that simplified looks like this only it has 2k rows. I'm wondering if there's a way to either extract data or filter data by the values in the LocationID column. I would like to extract rows that have the word "Creek" or "River" in them which would leave out the other names (such as the Banana Forest value).

 LocationID, Code
 Alk River, 232
 Bala River, 4324
 Banana Forest, 344
 Cake River, 432
 Alk Creek, 6767
 Cake Creek, 766

Thank you!

Upvotes: 0

Views: 2717

Answers (1)

akrun
akrun

Reputation: 887118

We can do this with tidyverse

library(dplyr)
library(stringr)
df1 %>% 
    filter(str_detect(LocationID, '\\b(River|Creek)\\b'))
#   LocationID Code
#1  Alk River  232
#2 Bala River 4324
#3 Cake River  432
#4  Alk Creek 6767
#5 Cake Creek  766

Upvotes: 2

Related Questions