问题:
id = c("a", "a", "a", "b", "b")
date = seq(as.Date("2015-12-01"), as.Date("2015-12-05"), by="days")
v1 = sample(seq(1, 20), 5)
v2 = sample(seq(1, 20), 5)
df = data.frame(id, date, v1, v2)
希望将这一份数据转化便成为以下的结构:
id date v1 v2 v1Cum v2Cum
a 2015-12-01 1 13 0 0
a 2015-12-02 7 11 1 13
a 2015-12-03 12 2 8 24
b 2015-12-04 18 6 0 0
b 2015-12-05 4 9 18 6
解决方案:
原始数据:
set.seed(123)
df = data.frame(
id = c("a", "a", "a", "b", "b"),
date = seq(as.Date("2015-12-01"), as.Date("2015-12-05"), by="days"),
v1 = sample(seq(1, 20), 5),
v2 = sample(seq(1, 20), 5),
v3 = sample(seq(1, 20), 5)
)
> df
id date v1 v2 v3
1 a 2015-12-01 6 1 20
2 a 2015-12-02 15 11 9
3 a 2015-12-03 8 17 13
4 b 2015-12-04 16 10 10
5 b 2015-12-05 17 8 2
实现代码:
df %>%
group_by(id) %>%
arrange(date) %>%
mutate_at(vars(v1:v3), funs(Cum = cumsum(lag(., default = 0)))) %>%
ungroup()
# A tibble: 5 x 8
# Groups: id [2]
id date v1 v2 v3 v1_Cum v2_Cum v3_Cum
<fctr> <date> <int> <int> <int> <int> <int> <int>
1 a 2015-12-01 6 1 20 0 0 0
2 a 2015-12-02 15 11 9 6 1 20
3 a 2015-12-03 8 17 13 21 12 29
4 b 2015-12-04 16 10 10 0 0 0
5 b 2015-12-05 17 8 2 16 10 10
网友评论