r - Subsetting vectors with extract -


imagine have vector, , want remove specific element. following

library(magrittr)  foo <- letters[1:10]  foo %>%  {    bar <- .     bar %>%       extract(bar %>%                equals("a") %>%                not) }   [1] "b" "c" "d" "e" "f" "g" "h" "i" "j" 

but if i'd more succinct, this:

foo %>%    extract(. %>%              equals("a") %>%              not) 

doesn't work:

error in extract(., . %>% equals("a") %>% not) :    invalid subscript type 'closure' 

isn't there more idiomatically magrittr'y way this?

one option pipe foo subsetting function [, limiting elements not equal using !=:

foo %>% "["(. != "a") # [1] "b" "c" "d" "e" "f" "g" "h" "i" "j" 

the magrittr package has aliased [ extract, equivalent to:

foo %>% extract(. != "a") # [1] "b" "c" "d" "e" "f" "g" "h" "i" "j" 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -