为非零值的运行创建组ID [英] Create group ID for runs of non-zero values
问题描述
我想在向量中找到连续的非零元素(由至少一个零分隔),并为每个组分配一个ID(随后的整数).
I would like to find contiguous runs of non-zero elements in a vector (separated by at least one zero), and to assign an ID to each group (subsequent integer).
玩具向量:
value <- c(1, 1, 2, 3, 4, 3, 0, 0, 0, 1, 2, 3, 9, 8, 0, 0, 3, 2)
在此示例中,存在三个非零值的游程:[1,1,2,3,4,3],[1,2,3,9,8],[3,2],分开按一个或多个零的块.
In this example, there are three runs of non-zero values: [1,1,2,3,4,3], [1,2,3,9,8], [3,2], separated by chunks of one or more zeros.
每个非零运行应具有唯一的ID:1、2、3 ...零运行应具有NA
作为ID:
Each non-zero run should have a unique ID: 1, 2, 3... Runs of zero should have NA
as ID:
value id
1 1 1
2 1 1
3 2 1
4 3 1
5 4 1
6 3 1
7 0 NA
8 0 NA
9 0 NA
10 1 2
11 2 2
12 3 2
13 9 2
14 8 2
15 0 NA
16 0 NA
17 3 3
18 2 3
推荐答案
使用rle()
.首先创建一个新的向量,用NA替换零.
Using rle()
. First create a new vector replacing the zeros with NA.
x <- match(value != 0, TRUE)
with(rle(!is.na(x)), {
lv <- lengths[values]
replace(x, !is.na(x), rep(seq_along(lv), lv))
})
# [1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
这篇关于为非零值的运行创建组ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!