Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In R, how can I filter out specific values in an array using dplyr's piping operator (%>%)?

How can I use the dplyr/magrittr piping operator (%>%) to filter/subset an input array and remove a specific value from that input array?

In more concrete terms, suppose I have the following array:

y = c('a','x','a','b','x','b','c','x','c')

Let’s say I want to remove all the occurrences of the 'x' item and get an array that looks like this: ('a','a','b','b','c','c'). How can I use the piping operator to do so?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is what I’ve tried so far and their respective results:

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.

It seems like the filter function doesn’t work for this, and I’m not sure what else does.

So, back to my main point: how can I use the piping operator (%>%) to subset/filter an array and remove certain unwanted values?

>Solution :

filter requires the first argument as .data which should be a data.frame/tibble etc. According to ?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.

So, the first two are not working because of the requirement. The last one needs the extraction step on the logical output

y %>%
   {.[. != "x"]}

-output

[1] "a" "a" "b" "b" "c" "c"

Or with magrittr alias

y %>% 
   magrittr::extract(. != "x")
[1] "a" "a" "b" "b" "c" "c"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading