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.
split() function is used to split the vector. cut() is the function that takes three parameters one parameter that is a vector with sequence along to divide the vector sequentially, second is the chunk number that is for number of chunks to be divided and the last is for labels to specify the chunks range.
To split the vector or data frame in R, use the split() function. To recover the split vector or data frame, use the unsplit() method.
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"
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With