dplyr过滤器(多种条件)功能缺少数据

时间:2020-06-29 21:19:03

标签: r dplyr tidyr

我正在尝试使用多个条件(完全匹配+部分匹配)来过滤我的数据集。但是feature_C中的---o---S---B (master) |----feature_A |----feature_C 函数仅返回符合条件的部分结果。这是一个示例:

filter

以下代码应返回行dplyrdf1 <- structure(list(Date = c("6/24/2020", "6/24/2020", "6/24/2020", "6/24/2020", "6/25/2020", "6/25/2020"), Market = c("A", "A", "A", "B", "B", "B"), Salesman = c("MF", "RP", "FR", "FR", "MF", "MF"), Product = c("* Apple", "Apple", "* Banana", "* Orange", "* Apple", "* Banana"), Quantity = c(20L, 15L, 20L, 20L, 10L, 15L), Price = c(1L,1L, 2L, 3L, 1L, 1L), Cost = c(0.5, 0.5, 0.5, 0.5, 0.6, 0.6)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6")) ,但仅返回第一行:

1

似乎是3引起的问题,但我需要它返回包含library(tidyr) df1 %>% filter(Salesman == c("MF","FR"), Market == "A", grepl("* ",Product)) 的{​​{1}}。

1 个答案:

答案 0 :(得分:2)

==仅可用于长度为1的向量。如果长度大于1,它将回收并导致意外输出。在这里,对于第一种情况,我们需要%in%,在grepl中,*是一个元字符(零个或多个)。转义(\\*)或将其放在方括号([*])中,或使用fixed = TRUE逐字求值。 fixed = TRUE可能更快,所以我们在这里使用

library(dplyr)
df1 %>% 
     filter(Salesman %in% c("MF", "FR"),
            Market == "A",  
            grepl("*", Product, fixed = TRUE))
#   Date Market Salesman  Product Quantity Price Cost
#1 6/24/2020      A       MF  * Apple       20     1  0.5
#3 6/24/2020      A       FR * Banana       20     2  0.5

如果应该从字符串的开头(*)拾取^,那么我们可以*对其进行转义

df1 %>% 
     filter(Salesman %in% c("MF", "FR"),
            Market == "A",  
            grepl("^\\*", Product))
#       Date Market Salesman  Product Quantity Price Cost
#1 6/24/2020      A       MF  * Apple       20     1  0.5
#3 6/24/2020      A       FR * Banana       20     2  0.5