基于块长度向量的分裂向量
Splitting vector based on vector of chunk-lengths
我有一个二进制数向量。我知道每组对象的连续长度;我如何根据该信息进行拆分(没有 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 组数字后的 4 个数字,1,0,0,0
,等等
有没有办法将其拆分并将拆分后的项目返回到列表中?
丑陋的方法是通过 for 循环跟踪当前的 cumsum,但我正在寻找一种更优雅的方法(如果有的话)。
可以使用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"
另一种选择是
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"
我有一个二进制数向量。我知道每组对象的连续长度;我如何根据该信息进行拆分(没有 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 组数字后的 4 个数字,1,0,0,0
,等等
有没有办法将其拆分并将拆分后的项目返回到列表中?
丑陋的方法是通过 for 循环跟踪当前的 cumsum,但我正在寻找一种更优雅的方法(如果有的话)。
可以使用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"
另一种选择是
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"