在dplyr过滤器中取反函数参数 [英] Negating a function parameter in dplyr filter

查看:60
本文介绍了在dplyr过滤器中取反函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望此函数能够对数据帧进行子集化,并列出不在子集中的行。下面是一个示例:

I want this function to be able to subset a data frame and to list rows that are NOT in the subset. Here's an example:

library(tidyverse)
library(stringr)
arrests <- USArrests %>% rownames_to_column()

list_arrests <- function(criteria) {
  arrests %>% filter(str_detect(rowname, criteria))
}

list_arrests("North|South")
list_arrests("New")

如何重新编写该函数,以便列出不是 北|南|新 的行?

How can I rewrite the function so that it will list rows that, say, are not "North|South|New"?

推荐答案

我们只需要在!) >过滤器

We just need to negate (!) in the filter

list_arrests <- function(criteria) {
   arrests %>% 
        filter(!str_detect(rowname, criteria))
 }

list_arrests("North|South|New")






可以在单个函数中通过添加另一个参数来完成


It can be done within a single function by adding another argument

list_arrests <- function(criteria, Negate = FALSE) {
 if(Negate){
     arrests %>% filter(!str_detect(rowname, criteria))
   } else arrests %>% filter(str_detect(rowname, criteria))
  }

list_arrests("North|South|New")
#      rowname Murder Assault UrbanPop Rape
#1  New Hampshire    2.1      57       56  9.5
#2     New Jersey    7.4     159       89 18.8
#3     New Mexico   11.4     285       70 32.1
#4       New York   11.1     254       86 26.1
#5 North Carolina   13.0     337       45 16.1
#6   North Dakota    0.8      45       44  7.3
#7 South Carolina   14.4     279       48 22.5
#8   South Dakota    3.8      86       45 12.8

list_arrests("North|South|New", Negate = TRUE)
#    rowname Murder Assault UrbanPop Rape
#1        Alabama   13.2     236       58 21.2
#2         Alaska   10.0     263       48 44.5
#3        Arizona    8.1     294       80 31.0
#4       Arkansas    8.8     190       50 19.5
#5     California    9.0     276       91 40.6
#6       Colorado    7.9     204       78 38.7
#7    Connecticut    3.3     110       77 11.1
#8       Delaware    5.9     238       72 15.8
#9        Florida   15.4     335       80 31.9
#10       Georgia   17.4     211       60 25.8
#11        Hawaii    5.3      46       83 20.2
#12         Idaho    2.6     120       54 14.2
#13      Illinois   10.4     249       83 24.0
#14       Indiana    7.2     113       65 21.0
#15          Iowa    2.2      56       57 11.3
#16        Kansas    6.0     115       66 18.0
#17      Kentucky    9.7     109       52 16.3
#18     Louisiana   15.4     249       66 22.2
#19         Maine    2.1      83       51  7.8
#20      Maryland   11.3     300       67 27.8
#21 Massachusetts    4.4     149       85 16.3
#22      Michigan   12.1     255       74 35.1
#23     Minnesota    2.7      72       66 14.9
#24   Mississippi   16.1     259       44 17.1
#25      Missouri    9.0     178       70 28.2
#26       Montana    6.0     109       53 16.4
#27      Nebraska    4.3     102       62 16.5
#28        Nevada   12.2     252       81 46.0
#29          Ohio    7.3     120       75 21.4
#30      Oklahoma    6.6     151       68 20.0
#31        Oregon    4.9     159       67 29.3
#32  Pennsylvania    6.3     106       72 14.9
#33  Rhode Island    3.4     174       87  8.3
#34     Tennessee   13.2     188       59 26.9
#35         Texas   12.7     201       80 25.5
#36          Utah    3.2     120       80 22.9
#37       Vermont    2.2      48       32 11.2
#38      Virginia    8.5     156       63 20.7
#39    Washington    4.0     145       73 26.2
#40 West Virginia    5.7      81       39  9.3
#41     Wisconsin    2.6      53       66 10.8
#42       Wyoming    6.8     161       60 15.6

这篇关于在dplyr过滤器中取反函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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