Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting vector based on vector of chunk-lengths

Tags:

chunks

r

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.

like image 897
user1234440 Avatar asked Jan 03 '15 00:01

user1234440


People also ask

How do I split a vector into chunks in R?

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.

How do I split a vector in R?

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.


2 Answers

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"
like image 196
Rich Scriven Avatar answered Sep 20 '22 18:09

Rich Scriven


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"
like image 24
akrun Avatar answered Sep 23 '22 18:09

akrun