为连续序列创建分组变量并分割向量 [英] Create grouping variable for consecutive sequences and split vector
问题描述
我有一个向量,例如 c(1, 3, 4, 5, 9, 10, 17, 29, 30)
我想将相邻"元素组合在一起在不规则的向量中形成一个规则的连续序列,即增加 1,导致:
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, i.e. an increase by 1, in a ragged vector resulting in:
L1:1
L2:3,4,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屋!