I feel I'm in the realm of the right answer but for the life of me I can't figure out how to do this. I want to take an entry on a product ledger and change the shape of the data where I have a row/case for every life month of a product transaction.
As a note, sometimes these ledger entries are reversed/refunded (and have date_from > date_thru). I have an example of a new purchase and a refund in the data frame below. Also, transaction dates are pre-cleaned to always be the first of the month in which they were made, since I only care about monthly recurring revenue (mrr) on a month-by-month basis.
Sample df:
user <- c("1001", "1002")
line_item <- c("abc123", "def456")
date_from <- as.Date(c("2015-01-01", "2015-06-01"), "%Y-%m-%d")
date_thru <- as.Date(c("2015-04-01", "2014-12-01"), "%Y-%m-%d")
mrr <- c(2.22, -4.44)
df <- cbind.data.frame(user, line_item, date_from, date_thru, mrr)
Output:
user line_item date_from date_thru mrr
1 1001 abc123 2015-01-01 2015-04-01 2.22
2 1002 def456 2015-06-01 2014-12-01 -4.44
Desired result:
user line_item month mrr
1001 abc123 2015-01-01 2.22
1001 abc123 2015-02-01 2.22
1001 abc123 2015-03-01 2.22
1001 abc123 2015-04-01 2.22
1002 def456 2015-06-01 -4.44
1002 def456 2015-05-01 -4.44
1002 def456 2015-04-01 -4.44
1002 def456 2015-02-01 -4.44
1002 def456 2015-01-01 -4.44
1002 def456 2014-12-01 -4.44
How can I create a new month
column by vectorizing a function like seq(date_from, date_thru, by="months")
, and then joining all the resulting vectors into one df again like above?
I've been trying lapply
, dplyr::mutate
, and seq
and can't get them to work correctly together.
Thanks in advance!