我有以下数据:
df <- data.frame(id = c("1", "1", "1", "1", "2", "2", "2"),
x = c(0, 1, 0, 1, 0, 1, 0))
id x
1 0
1 1
1 0
1 1
2 0
2 1
2 0
我想统计
1
中 x
的出现次数。所需的输出:
id x count
1 0 0
1 1 1
1 0 0
1 1 2
2 0 0
2 1 1
2 0 0
我该怎么做?首选
dplyr
解决方案。
library(tidyverse)
df <- tibble(id = c("1", "1", "1", "1", "2", "2", "2"),
x = c(0, 1, 0, 1, 0, 1, 0))
df |> mutate(
cumsum = cumsum(x),
tally = if_else(x == 0, 0, cumsum),
.by = id
)
#> # A tibble: 7 × 4
#> id x cumsum tally
#> <chr> <dbl> <dbl> <dbl>
#> 1 1 0 0 0
#> 2 1 1 1 1
#> 3 1 0 1 0
#> 4 1 1 2 2
#> 5 2 0 0 0
#> 6 2 1 1 1
#> 7 2 0 1 0
创建于 2023-11-04,使用 reprex v2.0.2
您可以获取
x
值的累积总和。
library(dplyr)
df %>%
mutate(count = replace(cumsum(x), x == 0, 0), .by = id)
# id x count
#1 1 0 0
#2 1 1 1
#3 1 0 0
#4 1 1 2
#5 2 0 0
#6 2 1 1
#7 2 0 0