Eric Hartford Eric Hartford - 4 months ago 8
R Question

how to do anonymous vector in R?

I can do this to get the even numbers from 1 to 10

x <- 1:10
x[x %% 2 == 0]
[1] 2 4 6 8 10


but I want to do it without the x.
I want to do something like:

1:10[_ %% 2 == 0]


is there syntax for this?

Answer

You can do it using %>%

library(magrittr)
1:10 %>% subset(.%% 2 == 0)
[1]  2  4  6  8 10