如何指定“不包含”在R中的dplyr过滤器 [英] How to specify "does not contain" in dplyr filter in R

查看:129
本文介绍了如何指定“不包含”在R中的dplyr过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用名为 SE_CSVLinelist_clean 的表,我想提取行其中变量称为 where_case_travelled_1 不包含字符串加拿大境外省外/居住地,但在加拿大境内。然后创建一个名为 SE_CSVLinelist_filtered 的新表。

  SE_CSVLinelist_filtered<  - 过滤器(SE_CSVLinelist_clean,
where_case_travelled_1%in%-c('Outside Canada','Outside province / territory of residence但in Canada')

当我使用c而不是-c时,上面的代码可以工作。

那么当我真的想排除包含国家或省份以外的行?



非常感谢

解决方案

请注意,%中的%返回一个逻辑向量 TRUE FALSE 。要否定它,您可以在逻辑语句前面使用

  SE_CSVLinelist_filtered<  -  filter(SE_CSVLinelist_clean,
!where_case_travelled_1%in%
c('Outside Canada','Outside province / residence of residence但在加拿大境内))

关于您原来的方法与 -c(...) - 是一个一元运算符,对数字或复数向量(或可强制给它们的对象)执行算术(从 help( - ))。由于您处理的字符向量不能强制为数字或复合,因此您不能使用 -


I am quite new to R.

Using the table called SE_CSVLinelist_clean, I want to extract the rows where the Variable called where_case_travelled_1 DOES NOT contain the strings "Outside Canada" OR "Outside province/territory of residence but within Canada". Then create a new table called SE_CSVLinelist_filtered.

SE_CSVLinelist_filtered <- filter(SE_CSVLinelist_clean, 
where_case_travelled_1 %in% -c('Outside Canada','Outside province/territory of residence but within Canada'))

The code above works when I just use "c" and not "-c".
So, how do I specify the above when I really want to exclude rows that contains that outside of the country or province?

Many thanks

解决方案

Note that %in% returns a logical vector of TRUE and FALSE. To negate it, you can use ! in front of the logical statement:

SE_CSVLinelist_filtered <- filter(SE_CSVLinelist_clean, 
 !where_case_travelled_1 %in% 
   c('Outside Canada','Outside province/territory of residence but within Canada'))

Regarding your original approach with -c(...), - is a unary operator that "performs arithmetic on numeric or complex vectors (or objects which can be coerced to them)" (from help("-")). Since you are dealing with a character vector that cannot be coerced to numeric or complex, you cannot use -.

这篇关于如何指定“不包含”在R中的dplyr过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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