[英]Error applying function in grouped tibble
I am trying to create a function that takes a current balance and then works backwards monthly to calculate prior month end balances from each month's additions and losses.我正在尝试创建一个 function,它获取当前余额,然后每月向后计算,以根据每个月的增加和损失计算上个月末的余额。 The balances need to be calculated within combinations of variables.
余额需要在变量组合中计算。 The output I want looks like this - month_end_balance is what I want the function to output.
我想要的 output 看起来像这样 - month_end_balance 是我想要的 function 到 output。 Everything else I have.
我所拥有的一切。
type![]() |
service![]() |
month_starting ![]() |
add![]() |
loss![]() |
current_balance ![]() |
month_end_balance![]() |
---|---|---|---|---|---|---|
A![]() |
Luxury![]() |
12/1/21 ![]() |
2 ![]() |
1 ![]() |
20 ![]() |
20 ![]() |
A![]() |
Luxury![]() |
11/1/21 ![]() |
4 ![]() |
7 ![]() |
NA![]() |
19 ![]() |
A![]() |
Luxury![]() |
10/1/21 ![]() |
0 ![]() |
0 ![]() |
NA![]() |
22 ![]() |
B![]() |
Economy![]() |
12/1/21 ![]() |
2 ![]() |
8 ![]() |
50 ![]() |
50 ![]() |
B![]() |
Economy![]() |
11/1/21 ![]() |
4 ![]() |
2 ![]() |
NA![]() |
56 ![]() |
B![]() |
Economy![]() |
10/1/21 ![]() |
0 ![]() |
0 ![]() |
NA![]() |
54 ![]() |
I created the following function, which works on ungrouped data.我创建了以下 function,它适用于未分组的数据。
running_balance_4 <- function(current_balance, add, loss) {
out <- rep(NA, length(current_balance))
out[[1]] <- current_balance[[1]]
for (i in 2:(length(current_balance))) {
out[[i]] <- out[[(i-1)]] - add[[(i-1)]] + loss[[(i-1)]]
}
out
}
But I can't get it to apply within each group.但我不能让它适用于每个组。 It might just be a syntax issue.
这可能只是语法问题。
df %>%
group_by(type, service) %>%
arrange(type, service, desc(month_starting)) %>%
group_modify(running_balance_4(current_balance, add, loss))
Appreciate any help with the syntax and/or the function itself.感谢语法和/或 function 本身的任何帮助。
Update: when I attempt to run it I get the following error message: object 'current_balance' not found.更新:当我尝试运行它时,我收到以下错误消息: object 'current_balance' not found。 So I think there may be a syntax error in addition to any issues with the function.
所以我认为除了 function 的任何问题之外,还可能存在语法错误。
I see in the comments you got your function to work.我在评论中看到你让你的 function 工作。 However, I thought you might want to see a solution using built in R functions rather than applying a grouped function.
但是,我认为您可能希望看到使用内置 R 函数而不是应用分组 function 的解决方案。
library(tibble)
library(dplyr)
df <- tribble(
~type, ~service, ~month_starting, ~add, ~loss, ~current_balance,
"A", "Luxury", "12/1/21", 2, 1, 20,
"A", "Luxury", "11/1/21", 4, 7, NA,
"A", "Luxury", "10/1/21", 0, 0, NA,
"B", "Economy", "12/1/21", 2, 8, 50,
"B", "Economy", "11/1/21", 4, 2, NA,
"B", "Economy", "10/1/21", 0, 0, NA
)
df %>%
# Two temporary columns to calculate with.
mutate(
# Replace current balance with 0 to work with cumulative sum.
c_balance = coalesce(current_balance, 0),
# Add the loss and subtract the add since we are working backwards.
monthly = c_balance + loss - add
) %>%
arrange(type, service, desc(month_starting)) %>%
group_by(type, service) %>%
# Taking the lag will put NA on the first element (the rows with current_balance)
# cumsum is a built in cumulative sum
mutate(monthly = lag(cumsum(monthly))) %>%
ungroup() %>%
mutate(month_end_balance = pmax(current_balance, monthly, na.rm = T)) %>%
select(-c_balance, -monthly)
It makes a difference based off of the current_balance
, does a cumulative sum of that difference using cumsum
, and then lines up the observation using lag
.它根据
current_balance
产生差异,使用cumsum
对该差异进行累积总和,然后使用lag
排列观察结果。 The output you were after can then be found by the maximum between current_balance
and the lagged variable, since all of the values you don't want are NA
.然后可以通过
current_balance
和滞后变量之间的最大值找到您所追求的 output ,因为您不想要的所有值都是NA
。
# A tibble: 6 × 7
type service month_starting add loss current_balance month_end_balance
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 A Luxury 12/1/21 2 1 20 20
2 A Luxury 11/1/21 4 7 NA 19
3 A Luxury 10/1/21 0 0 NA 22
4 B Economy 12/1/21 2 8 50 50
5 B Economy 11/1/21 4 2 NA 56
6 B Economy 10/1/21 0 0 NA 54
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.