Dplyr滚动平衡

时间:2017-04-19 15:47:50

标签: r dplyr

我正在尝试计算余额列。

所以,举一个例子,我想从此开始:

df <- data.frame(group = c("A", "A", "A", "A", "A"),
                  start = c(5, 0, 0, 0, 0),
                  receipt = c(1, 5, 6, 4, 6),
                  out = c(4, 5, 3, 2, 5))

> df
  group start receipt out
1     A     5       1   4
2     A     0       5   5
3     A     0       6   3
4     A     0       4   2
5     A     0       6   5

创建新的余额列,如下所示

> dfb
  group start receipt out balance
1     A     5       1   4       2
2     A     0       5   5       2
3     A     0       6   3       5
4     A     0       4   2       7
5     A     0       6   5       8

我尝试了以下尝试,但它没有工作

dfc <- df %>%
       group_by(group) %>%
       mutate(balance = if_else(row_number() == 1, start + receipt - out, (lag(balance) + receipt) - out)) %>%
       ungroup()

真的很感激一些帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用cumsum中的dplyr。注意:我必须更改您的初始df表格以匹配您所需结果中的表格,因为您在“out”中有不同的数据。

df <- data.frame(group = c("A", "A", "A", "A", "A"),
                  start = c(5, 0, 0, 0, 0),
                  receipt = c(1, 5, 6, 4, 6),
                  out = c(4, 5, 3, 2, 5))
dfc <- df %>%
       group_by(group) %>%
       mutate(balance=cumsum(start+receipt-out))

Source: local data frame [5 x 5]
Groups: group [1]

   group start receipt   out balance
  <fctr> <dbl>   <dbl> <dbl>   <dbl>
1      A     5       1     4       2
2      A     0       5     5       2
3      A     0       6     3       5
4      A     0       4     2       7
5      A     0       6     5       8