How to extract X amount of elements out of vector every X time

I have a large character with a length of 4800. The elements in the vector are not in the correct order so I would like to change that.

The order should be the first 96 elements, followed by the third 96 elements, followed by the fifth 96 elements, seventh 96 elements,... until the last 96 elements. Then it should be followed by the second 96 elements, the fourth 96 elements, the sixth 96 elements, .... until the last 96. So basically, extract 96 elements with a step of 96 until the end of the character.

So I could do it like this:


But this will take me a long time. Does anyone know how to solve this with one function?

Much appreciated!

1 answer

  • answered 2021-06-18 16:11 Onyambu

    According to your logic, if you need the first 96, then the third then the fifth we could do something like:

    icfinal[outer(1:96, 96 * seq(0, 4800/96, 2), "+")]