如何指定“不包含”?在dplyr过滤器中 [英] How to specify "does not contain" in dplyr filter
问题描述
我对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 calledwhere_case_travelled_1
DOES NOT contain the strings"Outside Canada"
OR"Outside province/territory of residence but within Canada"
. Then create a new table calledSE_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 ofTRUE
andFALSE
. 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)" (fromhelp("-")
). Since you are dealing with a character vector that cannot be coerced to numeric or complex, you cannot use-
.这篇关于如何指定“不包含”?在dplyr过滤器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!