使用dplyr过滤包含某个字符串的行 [英] Filtering row which contains a certain string using dplyr

查看:908
本文介绍了使用dplyr过滤包含某个字符串的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用包含字符串 RTB 的行作为标准过滤数据框。
我正在使用 dplyr

I have to filter a data frame using as criterion those row in which is contained the string RTB. I'm using dplyr.

d.del <- df %.%
  group_by(TrackingPixel) %.%
  summarise(MonthDelivery = as.integer(sum(Revenue))) %.%
  arrange(desc(MonthDelivery))

我知道我可以使用函数 filter dplyr 中,但我不完全如何告诉它来检查字符串的内容。

I know I can use the function filter in dplyr but I don't exactly how to tell it to check for the content of a string.

特别是我想查看 TrackingPixel 列中的内容。如果字符串包含标签 RTB ,我想从结果中删除该行。

In particular I want to check the content in the column TrackingPixel. If the string contains the label RTB I want to remove the row from the result.

推荐答案

这个问题的答案已经在上面的评论中被@latemail发布了。您可以使用正则表达式获取 filter 的第二个和后续参数,如下所示:

The answer to the question was already posted by the @latemail in the comments above. You can use regular expressions for the second and subsequent arguments of filter like this:

filter(df, !grepl("RTB",TrackingPixel))

既然你没有提供原始数据,我将使用 mtcars 数据集添加一个玩具示例。想象一下,您只对马自达或丰田生产的汽车感兴趣。

Since you have not provided the original data, I will add a toy example using the mtcars data set. Imagine you are only interested in cars produced by Mazda or Toyota.

mtcars$type <- rownames(mtcars)
filter(mtcars, grepl('Toyota|Mazda', type))

   mpg cyl  disp  hp drat    wt  qsec vs am gear carb           type
1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4      Mazda RX4
2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  Mazda RX4 Wag
3 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 Toyota Corolla
4 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1  Toyota Corona

如果你想这样做,即不包括丰田和马自达汽车,过滤器命令如下所示:

If you would like to do it the other way round, namely excluding Toyota and Mazda cars, the filter command looks like this:

filter(mtcars, !grepl('Toyota|Mazda', type))

这篇关于使用dplyr过滤包含某个字符串的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆