如何指定“不包含"在 dplyr 过滤器中 [英] How to specify "does not contain" in dplyr filter
问题描述
我对 R 很陌生.
使用名为 SE_CSVLinelist_clean
的表,我想提取名为 where_case_travelled_1
的变量不包含字符串 "Outside Canada"
的行或 居住在省/地区以外但在加拿大境内"
.然后创建一个名为 SE_CSVLinelist_filtered
的新表.
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'))
当我只使用c"而不是-c"时,上面的代码有效.
那么,当我真的想排除包含国家或省以外的行时,如何指定上述内容?
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?
推荐答案
注意 %in%
返回 TRUE
和 FALSE
的逻辑向量>.要否定它,您可以在逻辑语句前使用 !
:
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'))
关于您使用 -c(...)
的原始方法,-
是一个一元运算符,它对数字或复数向量(或可以为强迫他们)"(来自 help("-")
).由于您正在处理无法强制转换为数字或复数的字符向量,因此您不能使用 -
.
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屋!