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

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

问题描述

我对R很陌生。



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

  SE_CSVLinelist_filtered<-过滤器(SE_CSVLinelist_clean,
where_case_travelled_1%in%-c(加拿大境外,居住省份/地区,但在加拿大境内))

上面的代码在我只使用 c而不是 -c时有效。

所以,当我真的想排除时,如何指定以上代码

解决方案

请注意%in%返回逻辑向量 TRUE FALSE 。要取消它,可以在逻辑语句前使用

  SE_CSVLinelist_filtered<-过滤器(SE_CSVLinelist_clean,
!where_case_travelled_1%in%
c('加拿大境外','居住省/领地但不在加拿大境内'))

关于您使用 -c(...),<$ c的原始方法$ 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?

解决方案

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 -.

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

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