我有以下数据,它们是在给定日期前需要付款的。
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
)
我想为每笔款项创建一个零利息付款计划,在到期前的每个星期五付款。例如,对于 2024 年 11 月 26 日到期的 387,从现在(2024 年 11 月 5 日)到那时共有 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