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