传递一些参数以将向量分割为连续的段

时间:2015-11-21 03:13:27

标签: r

需要将长矢量分成连续不等大小的块。

subsum<-(c(1,2,3,4,3,2,1,3,3,4,5,5,6,7,7,7,8,8,9,0,0,0,9,8,7,6,6,5,4,4,5,2,2,3,2,2,3,2,3,3))
cuts<- c(list(5),list(15),list(20))
# I can do one as follows
split(subsum, ceiling(seq_along(subsum)/10))
# How could I do three sublists consecutively. The below example does not work. 
split(subsum, ceiling(seq_along(subsum)/seq_along(5,15,20)))

# Desired output is:  

[[1]]
1,2,3,4,3
[[2]] 
2,1,3,3,4,5,5,6,7,7,7,8,8,9,0
[[3]]
0,0,9,8,7,6,6,5,4,4,5,2,2,3,2,2,3,2,3,3

因为这是如何被赋予,因此以向量列表的形式输入IMPUT的问题

# The imput comes as a list of vectors with the following output in the form of I have changed the form. 
LL
[[1]]
[1] 5

[[2]]
[1] 15

[[3]]
[1] 20  

1 个答案:

答案 0 :(得分:2)

怎么样:

subsum <- c(1,2,3,4,3,2,1,3,3,4,5,5,6,7,7,7,8,8,9,0,0,0,9,8,7,6,6,5,4,4,5,2,2,3,2,2,3,2,3,3)
cuts   <- c(5,15,20)
group  <- rep(1:length(cuts), times=cuts)
split(subsum, group)