I have a binary number vector. I know the sequential length of each group of objects; How can I split based on this information (without a loop)?
x = c("1","0","1","0","0","0","0","0","1") .length = c(group1 = 2,group2=4, group3=3)
x is the binary number vector that I need to split. .length is the information I give. .length essentially tells me that the first group has 2 elements, and they are the first two elements of 1,0 . The second group has elements 4 and contains 4 numbers that follow the numbers of group 1, 1,0,0,0 , etc.
Is there a way to split this and return the split item to the list?
The ugly way is to track the current cumsum through a for loop, but I'm looking for a more elegant way, if any.
source share