基于块长度向量的分裂向量

时间:2015-01-03 00:14:22

标签: r chunks

我有一个二进制数字向量。我知道每组物体的连续长度;如何基于该信息进行拆分(没有for循环)?

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

x是我需要拆分的二进制数向量。 .length是我得到的信息。 .length基本上告诉我第一组有2个元素,它们是前两个元素1,0。第二组包含4个元素,并包含第1组数字1,0,0,0等后面的4个数字。

有没有办法拆分并将拆分的项目返回到列表中?

丑陋的方式是通过for循环跟踪当前的cumsum,但我正在寻找一种更优雅的方式,如果有的话。

2 个答案:

答案 0 :(得分:11)

您可以使用rep设置拆分变量,使用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"

如果您想将群组名称带到拆分列表中,可以更改rep以复制名称

split(x, rep.int(names(.length), .length))
# $group1
# [1] "1" "0"
#
# $group2
# [1] "1" "0" "0" "0"
#
# $group3
# [1] "0" "0" "1"

答案 1 :(得分:1)

另一种选择是

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

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

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

获取group names

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

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

#$group3
#[1] "0" "0" "1"