r - Extract every n-th + x subsequent elements from a vector -
i create vector in each element n-th element plus x following elements of vector.
for example, if have vector a:
a <- c(1,2,3,4,5,6,7,8,9,10)
my new vector b should have elements
b <- c(1,2,5,6,9,10)
meaning first 2 elements, third 2 elements etc.
any appreciated!
logical indexing recycling this:
a <- c(1,2,3,4,5,6,7,8,9,10) a[c(t,t,f,f)] ## [1] 1 2 5 6 9 10
from comment question:
n <- 4 x <- 2 a[c(rep(t, n-x), rep(f,x))] ## [1] 1 2 5 6 9 10
Comments
Post a Comment