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

▼魔方 西西 提交于 2019-12-29 09:34:07

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/44317226/how-to-filter-data-based-on-one-word-of-a-column-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!