Cumulative count of blocks of 1 with 0 separators in a binary vector in R

拈花ヽ惹草 提交于 2019-12-11 02:25:22

问题


I have a data frame with a binary vector that I want to do a cumulative count of. However I would like to count the 'groups of 1's' rather than each individual 1 and create a new vector of this count while retaining the 0 separating values. i.e.

df1 <- data.frame(c(0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1)

n   bin
1    0
2    1
3    1
4    1
5    1
6    0
7    0
8    0
9    1
10   1
11   1
12   1
13   1
14   0
15   0
16   0
17   1
18   1
19   1 

becomes

n   bin cumul
1    0     0
2    1     1
3    1     1
4    1     1
5    1     1
6    0     0
7    0     0
8    0     0
9    1     2
10   1     2
11   1     2
12   1     2
13   1     2
14   0     0
15   0     0
16   0     0
17   1     3
18   1     3
19   1     3

how do I go about this?


回答1:


You can use the rleid function from package data.table:

df1 <- data.frame(bin = c(0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1))
library(data.table)
setDT(df1)
df1[, cumul := rleid(bin)]
df1[bin == 0, cumul := 0]                  
df1[bin == 1, cumul := rleid(cumul)]  
#    bin cumul
# 1:   0     0
# 2:   1     1
# 3:   1     1
# 4:   1     1
# 5:   1     1
# 6:   0     0
# 7:   0     0
# 8:   0     0
# 9:   1     2
#10:   1     2
#11:   1     2
#12:   1     2
#13:   1     2
#14:   0     0
#15:   0     0
#16:   0     0
#17:   1     3
#18:   1     3
#19:   1     3



回答2:


Although somehow manual:

l <- rle(df1$c1)$lengths
v <- rle(df1$c1)$values
v2 <-  cumsum(v)
v2[duplicated(v2)] <- 0

df1$cumul <- rep(v2, times = l)
df1
   c1 cumul
1   0     0
2   1     1
3   1     1
4   1     1
5   1     1
6   0     0
7   0     0
8   0     0
9   1     2
10  1     2
11  1     2
12  1     2
13  1     2
14  0     0
15  0     0
16  0     0
17  1     3
18  1     3
19  1     3



回答3:


Yet another

x<-c(0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1)
d<-cumsum(diff(c(0,x))>0)
d[x==0]<-0
cbind(x,d)
      x d
 [1,] 0 0
 [2,] 1 1
 [3,] 1 1
 [4,] 1 1
 [5,] 1 1
 [6,] 0 0
 [7,] 0 0
 [8,] 0 0
 [9,] 1 2
[10,] 1 2
[11,] 1 2
[12,] 1 2
[13,] 1 2
[14,] 0 0
[15,] 0 0
[16,] 0 0
[17,] 1 3
[18,] 1 3
[19,] 1 3


来源:https://stackoverflow.com/questions/35227312/cumulative-count-of-blocks-of-1-with-0-separators-in-a-binary-vector-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!