0

Suppose I have a vector v1 with 20 elements and another vector indicating groups within that other vector

v2=c(3, 10, 7)

This means the first group is the 3 first elements of v1, the second group the next 10 elements and finally the last group the last 7 elements.

I using a loop from 1 to 3 (the number of groups)

for (j in 1:3) 

and need to choose the first 3 elements when j=1, the next 10 elements when j=2 and the last 7 when j=3

How could I do this inside the loop?

user2246905
  • 1,029
  • 1
  • 12
  • 31
  • What is your expected output? A `list`? A `data.frame`? Something else? –  Nov 09 '15 at 02:04
  • I just need to use for example the first 3 elements, so create a temporal vector with those 3 elements when j=1. – user2246905 Nov 09 '15 at 02:05
  • 2
    Based on the accepted answer, it gives `split(v1, rep(seq_along(v2), v2))`. –  Nov 09 '15 at 02:09

0 Answers0