R - 计算有序序列

时间:2016-02-07 14:40:59

标签: r count dplyr

我无法顺序计算变量的值。

我的数据看起来像这样

   IDPERS    value year divorce
1  371102  married    1       0
2  371102 divorced    2       1
3  371102 divorced    3       1
4  371102       NA    4       0
5  371102 divorced    5       1
6  371102 divorced    6       1
7  371102       NA    7       0
8  371102       NA    8       0
9  371102 divorced    9       1
10 371102 divorced   10       1
11 371102 divorced   11       1

我想要的是计算离婚,例如

   IDPERS    value year divorce divorceEp
1  371102  married    1       0         0
2  371102 divorced    2       1         1
3  371102 divorced    3       1         2
4  371102       NA    4       0         2
5  371102 divorced    5       1         3
6  371102 divorced    6       1         4
7  371102       NA    7       0         4
8  371102       NA    8       0         4
9  371102 divorced    9       1         5
10 371102 divorced   10       1         6
11 371102 divorced   11       1         7

任何线索如何才能正确计算这一系列剧集?

library(dplyr)
dta %>% mutate(1:sum(divorce)) 

谢谢

dta = structure(list(IDPERS = c(371102, 371102, 371102, 371102, 371102, 
371102, 371102, 371102, 371102, 371102, 371102), value = c("married", 
"divorced", "divorced", "NA", "divorced", "divorced", "NA", "NA", 
"divorced", "divorced", "divorced"), year = 1:11, divorce = c(0, 
1, 1, 0, 1, 1, 0, 0, 1, 1, 1), divorceEp = c(0, 1, 2, 2, 3, 4, 
4, 4, 5, 6, 7)), class = "data.frame", .Names = c("IDPERS", "value", 
"year", "divorce", "divorceEp"), row.names = c(NA, -11L))

1 个答案:

答案 0 :(得分:2)

您可以使用cumsum()

mutate(dta,divorceEp=cumsum(divorce))
   IDPERS    value year divorce divorceEp
1  371102  married    1       0         0
2  371102 divorced    2       1         1
3  371102 divorced    3       1         2
4  371102       NA    4       0         2
5  371102 divorced    5       1         3
6  371102 divorced    6       1         4
7  371102       NA    7       0         4
8  371102       NA    8       0         4
9  371102 divorced    9       1         5
10 371102 divorced   10       1         6
11 371102 divorced   11       1         7