>想象一下我有向量,我想删除一个特定的元素。我可以执行以下操作
library(magrittr)
foo <- LETTERS[1:10]
foo %>%
{
bar <- .
bar %>%
extract(bar %>%
equals("A") %>%
not)
}
[1] "B" "C" "D" "E" "F" "G" "H" "I" "J"
但如果我想更简洁一点,这个:
foo %>%
extract(. %>%
equals("A") %>%
not)
不起作用:
Error in extract(., . %>% equals("A") %>% not) :
invalid subscript type 'closure'
难道没有更惯用的magrittr
方法可以做到这一点吗?
一种选择是将foo
管道传输到子集函数[
,使用 !=
限制为不等于 A 的元素:
foo %>% "["(. != "A")
# [1] "B" "C" "D" "E" "F" "G" "H" "I" "J"
magrittr
包将[
别名为 extract
,因此这相当于:
foo %>% extract(. != "A")
# [1] "B" "C" "D" "E" "F" "G" "H" "I" "J"