如何将向量分成规则的连续序列组? [英] How to partition a vector into groups of regular, consecutive sequences?
问题描述
我有一个向量,例如 c(1、3、4、5、9、10、17、29、30)
,我想分组
I have a vector, such as c(1, 3, 4, 5, 9, 10, 17, 29, 30)
and I would like to group together the 'neighboring' elements that form a regular, consecutive sequence in a ragged vector resulting in:
L1:1
L2:3,4,在衣衫vector的向量中形成规则连续序列的邻居元素, 5
L3:9,10
L4:17
L5:29,30
L1: 1
L2: 3,4,5
L3: 9,10
L4: 17
L5: 29,30
(前C程序员的)天真代码:
Naive code (of an ex-C programmer):
partition.neighbors <- function(v)
{
result <<- list() #jagged array
currentList <<- v[1] #current series
for(i in 2:length(v))
{
if(v[i] - v [i-1] == 1)
{
currentList <<- c(currentList, v[i])
}
else
{
result <<- c(result, list(currentList))
currentList <<- v[i] #next series
}
}
return(result)
}
现在我明白了,
a )R不是C(尽管有花括号)
b)全局变量是纯邪恶的
c)这是获得结果的极其无效的方法
Now I understand that
a) R is not C (despite the curly brackets)
b) global variables are pure evil
c) that is a horribly inefficient way of achieving the result
,因此欢迎使用更好的解决方案。
, so any better solutions are welcome.
推荐答案
大量使用一些R惯用法:
Making heavy use of some R idioms:
> split(v, cumsum(c(1, diff(v) != 1)))
$`1`
[1] 1
$`2`
[1] 3 4 5
$`3`
[1] 9 10
$`4`
[1] 17
$`5`
[1] 29 30
这篇关于如何将向量分成规则的连续序列组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!