Home > Enterprise >  Finding out dates
Finding out dates

Time:07-16

I have a data frame which looks like this:

Subscription  MonthlyPayment  FirstPaymentDate    NumberofPayments 
     <chr>       <dbl>            <date>                     <int>
1 Netflix        12.99            2021-05-24                    21
2 Spotify         9.99            2021-08-17                     7
3 PureGym        19.99            2022-07-04                     9
4 DisneyPlus      7.99            2020-10-26                    11
5 AmazonPrime    34.99            2020-08-11                    73
6 Youtube        12.99            2020-09-27                    35

I want to find out future payment dates for each subscription service. For example Netflix has 21 monthly payments, so I want to list out all the monthly payment days from the first payment date. How would I do this for each subscription service, using dplyr?

CodePudding user response:

You can use dplyr and tidyr; I create a list of sequential payments (rowwise) and then unnest that list

library(dplyr)
library(tidyr)

df %>%
  rowwise() %>% 
  mutate(Payments = list(seq(FirstPaymentDate, by="month", length.out=NumberofPayments))) %>% 
  unnest(Payments)

Output:

# A tibble: 156 × 5
   Subscription MonthlyPayment FirstPaymentDate NumberofPayments Payments  
   <chr>                 <dbl> <date>                      <int> <date>    
 1 Netflix                13.0 2021-05-24                     21 2021-05-24
 2 Netflix                13.0 2021-05-24                     21 2021-06-24
 3 Netflix                13.0 2021-05-24                     21 2021-07-24
 4 Netflix                13.0 2021-05-24                     21 2021-08-24
 5 Netflix                13.0 2021-05-24                     21 2021-09-24
 6 Netflix                13.0 2021-05-24                     21 2021-10-24
 7 Netflix                13.0 2021-05-24                     21 2021-11-24
 8 Netflix                13.0 2021-05-24                     21 2021-12-24
 9 Netflix                13.0 2021-05-24                     21 2022-01-24
10 Netflix                13.0 2021-05-24                     21 2022-02-24
# … with 146 more rows

CodePudding user response:

You can add the months based on NumberofPayments directly to FirstPaymentDate. This approach does not require dplyr.

library(lubridate)
library(purrr)

df <- data.frame(sub = c("Netflix", "Spotify", "PureGym", "DisneyPlus", "AmazonPrime", "Youtube"),
                 mo_pay = c(12.99, 9.99, 19.99, 7.99, 34.99, 12.99),
                 dt_fpay = as.Date(c("2021-05-24", "2021-08-17", "2022-07-04", "2020-10-26", "2020-08-11", "2020-09-27")),
                 n_pay = c(21, 7, 9, 11, 73, 35))

pay_dt <- map(seq(nrow(df)),
              function(x) df$dt_fpay[x] %m % months(seq(df$n_pay[x])))

names(pay_dt) <- df$sub
pay_dt

output:

> pay_dt
$Netflix
 [1] "2021-06-24" "2021-07-24" "2021-08-24" "2021-09-24" "2021-10-24" "2021-11-24"
 [7] "2021-12-24" "2022-01-24" "2022-02-24" "2022-03-24" "2022-04-24" "2022-05-24"
[13] "2022-06-24" "2022-07-24" "2022-08-24" "2022-09-24" "2022-10-24" "2022-11-24"
[19] "2022-12-24" "2023-01-24" "2023-02-24"

$Spotify
[1] "2021-09-17" "2021-10-17" "2021-11-17" "2021-12-17" "2022-01-17" "2022-02-17"
[7] "2022-03-17"

$PureGym
[1] "2022-08-04" "2022-09-04" "2022-10-04" "2022-11-04" "2022-12-04" "2023-01-04"
[7] "2023-02-04" "2023-03-04" "2023-04-04"

$DisneyPlus
 [1] "2020-11-26" "2020-12-26" "2021-01-26" "2021-02-26" "2021-03-26" "2021-04-26"
 [7] "2021-05-26" "2021-06-26" "2021-07-26" "2021-08-26" "2021-09-26"

$AmazonPrime
 [1] "2020-09-11" "2020-10-11" "2020-11-11" "2020-12-11" "2021-01-11" "2021-02-11"
 [7] "2021-03-11" "2021-04-11" "2021-05-11" "2021-06-11" "2021-07-11" "2021-08-11"
[13] "2021-09-11" "2021-10-11" "2021-11-11" "2021-12-11" "2022-01-11" "2022-02-11"
[19] "2022-03-11" "2022-04-11" "2022-05-11" "2022-06-11" "2022-07-11" "2022-08-11"
[25] "2022-09-11" "2022-10-11" "2022-11-11" "2022-12-11" "2023-01-11" "2023-02-11"
[31] "2023-03-11" "2023-04-11" "2023-05-11" "2023-06-11" "2023-07-11" "2023-08-11"
[37] "2023-09-11" "2023-10-11" "2023-11-11" "2023-12-11" "2024-01-11" "2024-02-11"
[43] "2024-03-11" "2024-04-11" "2024-05-11" "2024-06-11" "2024-07-11" "2024-08-11"
[49] "2024-09-11" "2024-10-11" "2024-11-11" "2024-12-11" "2025-01-11" "2025-02-11"
[55] "2025-03-11" "2025-04-11" "2025-05-11" "2025-06-11" "2025-07-11" "2025-08-11"
[61] "2025-09-11" "2025-10-11" "2025-11-11" "2025-12-11" "2026-01-11" "2026-02-11"
[67] "2026-03-11" "2026-04-11" "2026-05-11" "2026-06-11" "2026-07-11" "2026-08-11"
[73] "2026-09-11"

$Youtube
 [1] "2020-10-27" "2020-11-27" "2020-12-27" "2021-01-27" "2021-02-27" "2021-03-27"
 [7] "2021-04-27" "2021-05-27" "2021-06-27" "2021-07-27" "2021-08-27" "2021-09-27"
[13] "2021-10-27" "2021-11-27" "2021-12-27" "2022-01-27" "2022-02-27" "2022-03-27"
[19] "2022-04-27" "2022-05-27" "2022-06-27" "2022-07-27" "2022-08-27" "2022-09-27"
[25] "2022-10-27" "2022-11-27" "2022-12-27" "2023-01-27" "2023-02-27" "2023-03-27"
[31] "2023-04-27" "2023-05-27" "2023-06-27" "2023-07-27" "2023-08-27"
  • Related