1

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"?

sid
  • 113
  • 1
  • 9

2 Answers2

0

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

data

set.seed(24)
v1 <- rnorm(30000)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you sir. Can you guide me me with this question https://stackoverflow.com/questions/45250893/my-objective-is-to-predict-the-next-3-events-of-each-id-num-based-on-their-previ – sid Jul 22 '17 at 08:58
0

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))]
M.F
  • 109
  • 5
  • Thanks. Can you guide me with this question https://stackoverflow.com/questions/45250893/my-objective-is-to-predict-the-next-3-events-of-each-id-num-based-on-their-previ – sid Jul 22 '17 at 09:00