7

I've got a vector of binary numbers. I know the consecutive length of each group of objects; how can I split based on that information (without for 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 that I am given. .length essentially tells me that the first group has 2 elements and they are the first two elements 1,0. The second group has 4 elements and contain the 4 numbers that follow the group 1 numbers, 1,0,0,0, etc.

Is there a way of splitting that and returning the splitted item in to a list?

The ugly way is to do with via a for loop keep track of the current cumsum, but I am looking for a more elegant way if there is one.

smci
  • 32,567
  • 20
  • 113
  • 146
user1234440
  • 22,521
  • 18
  • 61
  • 103

2 Answers2

12

You can use rep to set up the split-by variable, the use split

x = c("1","0","1","0","0","0","0","0","1")
.length = c(group1 = 2,group2=4, group3=3)

split(x, rep.int(seq_along(.length), .length))
# $`1`
# [1] "1" "0"
#
# $`2`
# [1] "1" "0" "0" "0"
#
# $`3`
# [1] "0" "0" "1"

If you wanted to take the group names with you to the split list, you can change rep to replicate the names

split(x, rep.int(names(.length), .length))
# $group1
# [1] "1" "0"
#
# $group2
# [1] "1" "0" "0" "0"
#
# $group3
# [1] "0" "0" "1"
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
1

Another option is

split(x,cumsum(sequence(.length)==1))
#$`1`
#[1] "1" "0"

#$`2`
#[1] "1" "0" "0" "0"

#$`3`
#[1] "0" "0" "1"

to get the group names

split(x, sub('.$', '', names(sequence(.length))))
#$group1
#[1] "1" "0"

#$group2
#[1] "1" "0" "0" "0"

#$group3
#[1] "0" "0" "1"
akrun
  • 874,273
  • 37
  • 540
  • 662