I have a df
identifying users and their recurring transactions in a subscription-based business model, where transactions can happen on a monthly, 3 months or 6 months cycle (every 30, 90, 180 days respectively).
library(tidyverse)
df <- tibble::tribble(
~user_id, ~date, ~plan, ~order_type,
1, "12/23/2018", "monthly", "acquisition",
1, "1/22/2019", "monthly", "repeat",
1, "2/21/2019", "3-months", "repeat",
1, "5/22/2019", "3-months", "repeat",
1, "8/20/2019", "3-months", "repeat",
2, "5/18/2019", "monthly", "acquisition",
2, "6/17/2019", "monthly", "repeat",
2, "7/17/2019", "monthly", "repeat",
3, "9/14/2018", "monthly", "acquisition",
3, "10/14/2018", "6-months", "repeat",
3, "4/12/2019", "6-months", "repeat",
4, "9/14/2018", "3-months", "acquisition",
4, "12/13/2018", "monthly", "repeat",
4, "1/12/2019", "monthly", "repeat"
)
Each customer (identified with a unique user_id
) is free to start with any of those "plan" and over the course of her/his subscription, can move from one plan to the other.
My objective is to identify - with a preference on Dplyr solutions:
- User upgrades (namely, her/his first order (= acquisition) is "monthly" but the subsequent are either "3-months" or "6-months")
- User downgrades (namely, her/his first order (=acquisition) is either "3-months" or "6-months" but the subsequent are "monthly")
My desired output is:
df_output <- tibble::tribble(
~user_id, ~date, ~plan, ~order_type, ~behavior_type,
1, "12/23/2018", "monthly", "acquisition", "-",
1, "1/22/2019", "monthly", "repeat", "-",
1, "2/21/2019", "3-months", "repeat", "upgrade",
1, "5/22/2019", "3-months", "repeat", "-",
1, "8/20/2019", "3-months", "repeat", "-",
2, "5/18/2019", "monthly", "acquisition", "-",
2, "6/17/2019", "monthly", "repeat", "-",
2, "7/17/2019", "monthly", "repeat", "-",
3, "9/14/2018", "monthly", "acquisition", "-",
3, "10/14/2018", "6-months", "repeat", "upgrade",
3, "4/12/2019", "6-months", "repeat", "-",
4, "9/14/2018", "3-months", "acquisition", "-",
4, "12/13/2018", "monthly", "repeat", "downgrade",
4, "1/12/2019", "monthly", "repeat", "-"
)