rvectordplyrmagrittr

dplyr filter vector by position in pipe %>%


From this excellent answer it is possible to filter a vector with dplyr in a pipe using magrittr package underneath like so:

library(dplyr)
c("D", "B", "C", "A", NA) %>% 
  .[matches("[^AB]", vars=.)]
#[1] "D" "C"

c("D", "B", "C", "A", NA) %>% 
  .[.!="A"]
# [1] "D" "B" "C" NA 

But I would like to filter by vector position so if I wanted the first two positions I would get:

#[1] "D" "B"    #want to filter position <= 2

If I wanted 1st and 4th position I would get:

#[1] "D" "A"    #want to filter position by c(1, 4)

etc....

What is the best way to achieve this?

I thought using row_number might work but then remembered this wouldn't work as the sequence isn't right:

row_number(c("D", "B", "C", "A", NA))
# [1]  4  2  3  1 NA

I would be better using seq_along:

seq_along(c("D", "B", "C", "A", NA))
# [1] 1 2 3 4 5

But I'm not sure how to incorporate that in a pipe though.

Any ideas?

Thanks


Solution

  • The term "filtering" are not normally used to describe vector operations. Base R has a great tool for vector indexing/extracting: [

    There is zero reason to involve the magrittr pipe here. dplyr verbs like filter() have no real world utility for vector operations, they are intended to be used exclusively for manipulating tbl objects.

    In your examples you got tidyselect helpers involved, but that is insane overkill for doing basic positional matching.

    
    > my_vector <- c("D", "B", "C", "A", NA)
    
    > my_vector[c(1,2)]
    [1] "D" "B"
    
    > my_vector[c(1,4)]
    [1] "D" "A"