簡體   English   中英

在分組的小標題中應用 function 時出錯

[英]Error applying function in grouped tibble

我正在嘗試創建一個 function,它獲取當前余額,然后每月向后計算,以根據每個月的增加和損失計算上個月末的余額。 余額需要在變量組合中計算。 我想要的 output 看起來像這樣 - month_end_balance 是我想要的 function 到 output。 我所擁有的一切。

類型 服務 month_starting 添加 失利 current_balance 月末余額
一個 奢華 21 年 12 月 1 日 2 1 20 20
一個 奢華 21 年 11 月 1 日 4 7 不適用 19
一個 奢華 21 年 10 月 1 日 0 0 不適用 22
經濟 21 年 12 月 1 日 2 8 50 50
經濟 21 年 11 月 1 日 4 2 不適用 56
經濟 21 年 10 月 1 日 0 0 不適用 54

我創建了以下 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
}

但我不能讓它適用於每個組。 這可能只是語法問題。

df %>%
  group_by(type, service) %>%
  arrange(type, service, desc(month_starting)) %>%
  group_modify(running_balance_4(current_balance, add, loss))

感謝語法和/或 function 本身的任何幫助。

更新:當我嘗試運行它時,我收到以下錯誤消息: object 'current_balance' not found。 所以我認為除了 function 的任何問題之外,還可能存在語法錯誤。

我在評論中看到你讓你的 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)

它根據current_balance產生差異,使用cumsum對該差異進行累積總和,然后使用lag排列觀察結果。 然后可以通過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM