I have a vector containing say 30000 elements.
I want to get a new vector out of it which will have 15000 elements from index 1:5, 11:15, 21:25 and so on till 29991:29995.
How can I do it using "R programming"?
I have a vector containing say 30000 elements.
I want to get a new vector out of it which will have 15000 elements from index 1:5, 11:15, 21:25 and so on till 29991:29995.
How can I do it using "R programming"?
We can do
r1 <- unlist(lapply(seq(1, length(v1), by = 10), function(i) v1[i:(i+4)]))
Or a vectorized option would be
r2 <- v1[(seq_along(v1))*rep(c(TRUE, FALSE), c(5, 5))]
identical(r1, r2)
#[1] TRUE
set.seed(24)
v1 <- rnorm(30000)
use this code. . first check
x<-1:100
(y<-x[c(rep(T,5),rep(F,5))])
according to that
x<-rnorm(30000)
y<-x[c(rep(T,5),rep(F,5))]