在 R 中,如何使用 dplyr 的管道运算符 (%>%) 过滤掉数组中的特定值?
In R, how can I filter out specific values in an array using dplyr's piping operator (%>%)?
如何使用 dplyr
/magrittr
管道运算符 (%>%
) 到 filter/subset 输入数组并从该输入数组中删除特定值?
更具体地说,假设我有以下数组:
y = c('a','x','a','b','x','b','c','x','c')
假设我想删除所有出现的 'x'
项目并得到一个如下所示的数组:('a','a','b','b','c','c')
。我如何使用管道运算符来执行此操作?
以下是我到目前为止所做的尝试及其各自的结果:
result = y %>%
filter(.!='x')
# This yields an error:
# Error in UseMethod("filter") :
# no applicable method for 'filter' applied to an object of class "character"
result = y %>%
{filter(., .!='x')}
# This also yields an error:
# Error in UseMethod("filter") :
# no applicable method for 'filter' applied to an object of class "character"
result = y %>%
`[` %>% {.!='x'}
print(result)
# [1] TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE
# This doesn't throw an error, but it also doesn't work. It just gives me the
# TRUE/FALSE array that represents which items to keep or discard.
似乎 filter
函数对此不起作用,我不确定还有什么作用。
所以,回到我的要点:我如何使用管道运算符 (%>%
) 来 subset/filter 数组并删除某些不需要的值?
filter
要求第一个参数为 .data
,它应该是一个数据。frame/tibble 等。根据 ?filter
filter(.data, ..., .preserve = FALSE)
.data
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.
因此,由于要求,前两个无法正常工作。最后一个需要逻辑输出上的提取步骤
y %>%
{.[. != "x"]}
-输出
[1] "a" "a" "b" "b" "c" "c"
或使用 magrittr
别名
y %>%
magrittr::extract(. != "x")
[1] "a" "a" "b" "b" "c" "c"
如何使用 dplyr
/magrittr
管道运算符 (%>%
) 到 filter/subset 输入数组并从该输入数组中删除特定值?
更具体地说,假设我有以下数组:
y = c('a','x','a','b','x','b','c','x','c')
假设我想删除所有出现的 'x'
项目并得到一个如下所示的数组:('a','a','b','b','c','c')
。我如何使用管道运算符来执行此操作?
以下是我到目前为止所做的尝试及其各自的结果:
result = y %>%
filter(.!='x')
# This yields an error:
# Error in UseMethod("filter") :
# no applicable method for 'filter' applied to an object of class "character"
result = y %>%
{filter(., .!='x')}
# This also yields an error:
# Error in UseMethod("filter") :
# no applicable method for 'filter' applied to an object of class "character"
result = y %>%
`[` %>% {.!='x'}
print(result)
# [1] TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE
# This doesn't throw an error, but it also doesn't work. It just gives me the
# TRUE/FALSE array that represents which items to keep or discard.
似乎 filter
函数对此不起作用,我不确定还有什么作用。
所以,回到我的要点:我如何使用管道运算符 (%>%
) 来 subset/filter 数组并删除某些不需要的值?
filter
要求第一个参数为 .data
,它应该是一个数据。frame/tibble 等。根据 ?filter
filter(.data, ..., .preserve = FALSE)
.data A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.
因此,由于要求,前两个无法正常工作。最后一个需要逻辑输出上的提取步骤
y %>%
{.[. != "x"]}
-输出
[1] "a" "a" "b" "b" "c" "c"
或使用 magrittr
别名
y %>%
magrittr::extract(. != "x")
[1] "a" "a" "b" "b" "c" "c"