过滤包含特定字符串的行 [英] Filter rows which contain a certain string
问题描述
我必须使用包含字符串 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
函数,但我不知道如何告诉它检查字符串的内容.
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
如果你想反过来做,即不包括丰田和马自达汽车,filter
命令看起来像这样:
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屋!