I have a data frame with variable v1. I want a variable v2 to be the running count of zeros at the row (i.e. only count zeros at and before that row). Conversley, I'd like v3 to be the running count of non-zero numbers. The data frame dat illustrate the desired output.

dat <- structure(list(v1 = c(0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 10L, 
2L, 0L), v2 = c(1L, 2L, 2L, 3L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L
), v3 = c(0L, 0L, 1L, 1L, 1L, 1L, 2L, 3L, 4L, 5L, 6L, 6L)), .Names = c("v1", 
"v2", "v3"), class = "data.frame", row.names = c(NA, -12L))

> dat
   v1 v2 v3
1   0  1  0
2   0  2  0
3   1  2  1
4   0  3  1
5   0  4  1
6   0  5  1
7   1  5  2
8   1  5  3
9   1  5  4
10 10  5  5
11  2  5  6
12  0  6  6
有帮助吗?

解决方案

You want to use cumsum here, for the cumulative sum(s) of a given condition.

> v1 <- c(0, 0, 1, 0, 0, 0, 1, 1, 1, 10, 2, 0)
> v2 <- cumsum(v1 == 0)
> v3 <- cumsum(v1 > 0)
> data.frame(v1, v2, v3)
##    v1 v2 v3
## 1   0  1  0
## 2   0  2  0
## 3   1  2  1
## 4   0  3  1
## 5   0  4  1
## 6   0  5  1
## 7   1  5  2
## 8   1  5  3
## 9   1  5  4
## 10 10  5  5
## 11  2  5  6
## 12  0  6  6

其他提示

within(dat, {
  v33 <- cumsum(v1 != 0)
  v22 <- cumsum(v1 == 0)
})

#    v1 v2 v3 v22 v33
# 1   0  1  0   1   0
# 2   0  2  0   2   0
# 3   1  2  1   2   1
# 4   0  3  1   3   1
# 5   0  4  1   4   1
# 6   0  5  1   5   1
# 7   1  5  2   5   2
# 8   1  5  3   5   3
# 9   1  5  4   5   4
# 10 10  5  5   5   5
# 11  2  5  6   5   6
# 12  0  6  6   6   6
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top