根据简单的日期和金额计算一系列还款

问题描述 投票:0回答:1

我有以下数据,这些数据是给定日期所需的付款。

data <- tribble(
  ~Date, ~Amount,
  "26/11/2024", 387,
  "04/01/2025", 879,
  "08/02/2025", 954,
  "22/02/2025", 1849,
  "25/02/2025", 103,
  "05/03/2025", 2400
)

我想在金额到期前的每个星期五为每笔金额创建零利息付款计划。例如,对于 26/11/2024 到期的 387,从现在 (05/11/2024) 到那时有 3 个星期五。所以我预计在付款到期之前每个星期五都会有 387/3。

我尝试过以下代码:

library(tidyverse)
library(lubridate)

# calculate a sequence of dates from now until latest payment date
all_dates <- seq(as.Date("2024-11-05"),as.Date('2025-03-05'),by = 1) %>% 
  tibble() %>%
  rename("list_of_dates" = ".") %>% 
  mutate(dummy_amt = 0)

# create Friday payment plan
data2 <- data %>% 
  mutate(Date = strptime(as.character(Date), "%d/%m/%Y")) %>% 
  mutate(Date = format(Date, "%Y-%m-%d")) %>% 
  mutate(Date = as.Date(Date)) %>% 
  right_join(all_dates, by = c("Date" = "list_of_dates")) %>% 
  select(-dummy_amt) %>% 
  mutate(Amount = replace_na(Amount, 0)) %>% 
  arrange(Date) %>% 
  mutate(Day = wday(Date, label = TRUE, abbr = FALSE)) %>% 
  filter(Day == "Friday" | Amount != 0) %>% 
  mutate(no_fridays = if_else(Day == "Friday", 1, 0)) %>% 
  mutate(cum_fridays = cumsum(no_fridays))

...并且已经达到了我可以计算到特定付款日期的周五累计金额的程度。但我正在努力进行下一步,即将付款分配给之前的每个星期五。

这样的事情是理想的(例如对于前 2 笔金额)。然后我可以计算总列。

   Date       Amount Day     cum_fridays payment1 payment2
   <date>      <dbl> <ord>         <dbl>    <dbl>    <dbl>
 1 2024-11-08      0 Friday            1      129     97.7
 2 2024-11-15      0 Friday            2      129     97.7
 3 2024-11-22      0 Friday            3      129     97.7
 4 2024-11-26    387 Tuesday           3        0        0
 5 2024-11-29      0 Friday            4        0     97.7
 6 2024-12-06      0 Friday            5        0     97.7
 7 2024-12-13      0 Friday            6        0     97.7
 8 2024-12-20      0 Friday            7        0     97.7
 9 2024-12-27      0 Friday            8        0     97.7
10 2025-01-03      0 Friday            9        0     97.7
11 2025-01-04    879 Saturday          9        0        0
r dplyr
1个回答
0
投票

不是最简洁的方式,但很有趣:

l = lapply(df0$Date, \(i) {
  d = seq.Date(from = Sys.Date(), to = i, by=1)
  d[weekdays(d) == "Friday"]
})

M = Map("data.frame", payday = l, payroll = df0$Amount / lengths(l))
aggregate(. ~ payday, Reduce(\(...) merge(..., all = TRUE), M), sum) 
# do.call("rbind", M) would be work as well

给予

       payday  payroll
1  2024-11-08 557.9860
2  2024-11-15 557.9860
3  2024-11-22 557.9860
4  2024-11-29 428.9860
5  2024-12-06 428.9860
6  2024-12-13 428.9860
7  2024-12-20 428.9860
8  2024-12-27 428.9860
9  2025-01-03 428.9860
10 2025-01-10 331.3193
11 2025-01-17 331.3193
12 2025-01-24 331.3193
13 2025-01-31 331.3193
14 2025-02-07 331.3193
15 2025-02-14 263.1765
16 2025-02-21 263.1765
17 2025-02-28 141.1765

注意

df0 = structure(list(Date = structure(c(20053, 20092, 20127, 20141, 
20144, 20152), class = "Date"), Amount = c(387, 879, 954, 1849, 
103, 2400)), row.names = c(NA, -6L), class = "data.frame")
© www.soinside.com 2019 - 2024. All rights reserved.