在一列中筛选包含精确字符串的行。

huangapple go评论79阅读模式
英文:

Filter rows that contain exact strings in a column

问题

  1. df <- df %>% filter(grepl('\\<air\\>', y))
英文:

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

  1. library(dplyr)
  2. df &lt;- data.frame(x = c(13, 34, 5, 124, 56),
  3. y = c(&#39;air transport&#39;, &#39;hairdressing&#39;, &#39;airport&#39;, &#39;repair&#39;, &#39;frontend&#39;))

This gives me rows that partially contain air

  1. df %&gt;% filter(grepl(&#39;air&#39;,y))
  2. x y
  3. 1 13 air transport
  4. 2 34 hairdressing
  5. 3 5 airport
  6. 4 124 repair

Here is my intended result

  1. x y
  2. 1 13 air transport

答案1

得分: 1

使用\\b来匹配零长度的单词边界:

  1. df %>%
  2. filter(grepl('\\bair\\b', y))
英文:

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

  1. df %&gt;%
  2. filter(grepl(&#39;\\bair\\b&#39;, y))

huangapple
  • 本文由 发表于 2023年3月3日 17:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定