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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -