这是我的数据:
df <- tibble::tribble(
~Group, ~Year, ~given, ~required,
"A", 2017L, 3L, 1L,
"A", 2017L, 4L, 2L,
"A", 2017L, 8L, 6L,
"A", 2018L, 1L, 7L,
"A", 2018L, 4L, 10L,
"B", 2018L, 8L, 1L,
"B", 2019L, 3L, 4L,
"B", 2019L, 4L, 5L)
我要计算“必需”,以便对于任何“组”:
如何使用“组”,“年份”和“给定”变量来计算“必需”变量?
答案 0 :(得分:1)
library(tidyverse)
df2 <- df %>%
group_by(Group) %>%
mutate(required = c(1, diff(given)),
required = ifelse(required < 0, max(given) - abs(required), required),
required = cumsum(required)) %>%
ungroup()
df2
# # A tibble: 8 x 4
# Group Year given required
# <chr> <int> <int> <dbl>
# 1 A 2017 3 1
# 2 A 2017 4 2
# 3 A 2017 8 6
# 4 A 2018 1 7
# 5 A 2018 4 10
# 6 B 2018 8 1
# 7 B 2019 3 4
# 8 B 2019 4 5