I have the following problem and I can't find any easy solution.
我有以下问题,我找不到任何简单的解决方案。
I would like to find groups of nonzero elements in a vector (separated by at least one zero) and to assign an id to each group (subsequent integer). For example, in vector value <- c(1,1,2,3,4,3,0,0,0,1,2,3,9,8,0,0,3,2)
there should be three groups:[1,1,2,3,4,3],[1,2,3,9,8],[3,2], so I would like to obtain such a data frame:
我想在向量中找到非零元素组(由至少一个零分隔)并为每个组分配一个id(后续整数)。例如,在向量值< - 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],所以我想获得这样一个数据框:
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
5 个解决方案
#1
6
Using rle()
. First create a new vector replacing the zeros with NA.
使用rle()。首先创建一个用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
#2
11
You can try:
你可以试试:
as.integer(factor(cumsum(value==0)*NA^(value==0)))
#[1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
#3
1
Another possibility:
ifelse(value != 0,
cumsum(value != 0 & dplyr::lag(value) %in% c(0, NA)),
NA)
# [1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
#4
1
You could also do this:
你也可以这样做:
id <- (value>0)^NA
x <- rle(value>0)$lengths[c(TRUE, FALSE)]
id[!is.na(id)] <- rep(seq_along(x), times=x)
#[1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
#5
-2
- You need to define a vector of vectors so in v[0] you will find all values of the first group, and in v[1] you will find all values of the second group and so on,
- You need to loop on all values when you find a zero value. Continue until you find anon zero. Increment the vector with one and add the value and so on,
你需要定义一个向量向量,所以在v [0]中你会找到第一组的所有值,在v [1]中你会找到第二组的所有值,依此类推,
找到零值时,需要循环所有值。继续,直到你找到零。使用一个增加向量并添加值,依此类推,
I wish this answer be helpful.
我希望这个答案有所帮助。
#1
6
Using rle()
. First create a new vector replacing the zeros with NA.
使用rle()。首先创建一个用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
#2
11
You can try:
你可以试试:
as.integer(factor(cumsum(value==0)*NA^(value==0)))
#[1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
#3
1
Another possibility:
ifelse(value != 0,
cumsum(value != 0 & dplyr::lag(value) %in% c(0, NA)),
NA)
# [1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
#4
1
You could also do this:
你也可以这样做:
id <- (value>0)^NA
x <- rle(value>0)$lengths[c(TRUE, FALSE)]
id[!is.na(id)] <- rep(seq_along(x), times=x)
#[1] 1 1 1 1 1 1 NA NA NA 2 2 2 2 2 NA NA 3 3
#5
-2
- You need to define a vector of vectors so in v[0] you will find all values of the first group, and in v[1] you will find all values of the second group and so on,
- You need to loop on all values when you find a zero value. Continue until you find anon zero. Increment the vector with one and add the value and so on,
你需要定义一个向量向量,所以在v [0]中你会找到第一组的所有值,在v [1]中你会找到第二组的所有值,依此类推,
找到零值时,需要循环所有值。继续,直到你找到零。使用一个增加向量并添加值,依此类推,
I wish this answer be helpful.
我希望这个答案有所帮助。