In order to explain my question better, I have prepared the following sample data that looks similar to my original dataset:
library(zoo)
sample_data <- data.frame(User = c("customer1", "customer2", "customer3", "customer4", "customer5", "customer1", "customer2", "customer3", "customer4", "customer1", "customer3", "customer5"),
Cohort = as.yearmon(c("2020-03-01", "2020-02-17", "2020-04-10","2020-02-01", "2020-04-10", "2020-03-01", "2020-02-17", "2020-04-10","2020-02-01", "2020-03-01", "2020-04-10", "2020-04-30"), "%Y-%m-%d"),
Purchase_month = as.yearmon(c("2020-03-01", "2020-02-17", "2020-04-10", "2020-02-01", "2020-04-10", "2020-07-05", "2020-03-05", "2020-06-11","2020-03-07", "2020-11-01", "2020-11-04", "2020-06-30"), "%Y-%m-%d"),
Revenue = c(25, 34, 20, 50, 75, 80, 100, 76, 39, 20, 10, 90))
As you can see, I have purchase data with a column indicating the customer, one with the Cohort they belong to (the month they placed their very first order), another column where you can find their their purchase dates and one with their spend for each purchase.
What I would like to do is to change the table in order to show the Cohorts per row and the revenue per cohort per month in each column. The outcome should basically look like the following
Cohort | Feb 2020 | Mar 2020 | Apr 2020 | May 2020 | Jun 2020 | Jul 2020 | Aug 2020 | Sep 2020 | Oct 2020 | Nov 2020 | Dec 2020
Feb 2020 | 84 | 139 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
Mar 2020 | 0 | 25 | 0 | 0 | 0 | 80 | 0 | 0 | 0 | 20 | 0
Apr 2020 | 0 | 0 | 95 | 0 | 169 | 0 | 0 | 0 | 0 | 10 | 0
Thank you!
Ps: Maybe the title is not quite fitting, but I didn't know how such a transformation of the table is called.