我正在尝试创建dplyr管道以进行过滤
想象一下一个数据帧jobs
,我想在其中过滤titles
列中最高级的位置:
titles
Chief Executive Officer
Chief Financial Officer
Chief Technical Officer
Manager
Product Manager
Programmer
Scientist
Marketer
Lawyer
Secretary
用于过滤掉它们(最多为“经理”)的R代码将是...
jobs %>%
filter(!str_detect(title, 'Chief')) %>%
filter(!str_detect(title, 'Manager')) ...
但是我仍然希望在最后的过滤中保留“程序管理器”,以产生一个包含所有“低级作业”的新数据框,例如
Product Manager
Programmer
Scientist
Marketer
Lawyer
Secretary
有没有一种方法可以在给定值EXCEPT上为一个特定字符串指定str_detect()过滤器?
假定数据框的列具有1000个角色,并且具有包括“ Manager”在内的各种字符串组合,但是始终会针对特定异常进行过滤。
答案 0 :(得分:1)
或者您可以为{Product Manager“单独设置一个filter
library(tidyverse)
jobs %>%
filter((!str_detect(title, "Chief|Manager")) | str_detect(title, "Product Manager"))
# title
#1 Product Manager
#2 Programmer
#3 Scientist
#4 Marketer
#5 Lawyer
#6 Secretary
也可以使用grepl/grep
jobs[c(grep("Product Manager",jobs$title),
grep("Chief|Manager", jobs$title, invert = TRUE)),, drop = FALSE]