I have 2 kinds of posts (sponsored and non sponsored) for many users. I know the date when each post happened. Now I need to know for each post of a user when the last sponsored post happened (in full days) see the column "Days since last sponsored". Thank you for your help! I want to solve it in R.
| Date | User | Sponsored | Days Since Last Sponsored |
|---|---|---|---|
| 08.07.2022 | YYY | 0 | NA |
| 24.07.2022 | YYY | 1 | NA |
| 08.08.2022 | YYY | 1 | 15 |
| 15.08.2022 | YYY | 0 | 7 |
| 20.08.2022 | YYY | 0 | 12 |
| 14.05.2022 | ZZZ | 1 | NA |
| 15.05.2022 | ZZZ | 0 | 1 |
| 20.06.2022 | ZZZ | 0 | 36 |
| 20.06.2022 | ZZZ | 0 | 36 |
| 22.06.2022 | ZZZ | 1 | 38 |
| 22.06.2022 | ZZZ | 0 | 0 |
I have tried to group by user and use diff time. However the sponsored / non-sponsored gives me a headache.
Here's an option with
tidyverse:The calculation at the end is a bit off - I spot checked one of my values and it seems correct, but let me know if there's some other logic used.