I have a dataframe like this:
customer= c('1530','1530','1530','1531','1531','1532')
month = c('2021-10-01','2021-11-01','2021-12-01','2021-11-01','2021-12-01','2021-12-01')
month_number = c(1,2,3,1,2,1)
df <- data.frame('customer_id'=customer, entry_month=month)
df
| customer_id| entry_month|
| ---------- | ---------- |
1| 1530 | 2021-10-01 |
2| 1530 | 2021-11-01 |
3| 1530 | 2021-12-01 |
4| 1531 | 2021-11-01 |
5| 1531 | 2021-12-01 |
6| 1532 | 2021-12-01 |
I need to create a column that indicates the number of the month since the customer joined. Here is my desired output:
new_df <- data.frame('customer_id'=customer, 'month'=month, 'month_number'=month_number)
new_df
| customer_id| entry_month| month_number |
| ---------- | ---------- |--------------|
1| 1530 | 2021-10-01 | 1 |
2| 1530 | 2021-11-01 | 2 |
3| 1530 | 2021-12-01 | 3 |
4| 1531 | 2021-11-01 | 1 |
5| 1531 | 2021-12-01 | 2 |
6| 1532 | 2021-12-01 | 1 |