筛选包含特定字符串的行 [英] Filter rows which contain a certain string

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

问题描述

我必须使用包含字符串 RTB 的那些行作为标准来过滤数据帧。

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

我是使用 dplyr

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

我知道我可以在 dplyr filter $ c>,但我不完全知道如何告诉它检查字符串的内容。

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:

dplyr::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)
dplyr::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:

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

这篇关于筛选包含特定字符串的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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