英文:
Filter rows that contain exact strings in a column
问题
df <- df %>% filter(grepl('\\<air\\>', y))
英文:
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
x y
1 13 air transport
答案1
得分: 1
使用\\b
来匹配零长度的单词边界:
df %>%
filter(grepl('\\bair\\b', y))
英文:
Use \\b
to match a zero-length word boundary:
df %>%
filter(grepl('\\bair\\b', y))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论