I'm trying to calculate the dates between purchases and then the next expected date of purchase. I'm not sure how to deal with cases when it's the first purchase, the code currently gives NA which is accurate as you can't work out previous purchase if it's the first one. Instead of NA I'd prefer it to give the date of purchase. I've been trying to use case when but got stuck. Any suggestions?
So, here's a small reprex with dummy data, since (obviously) I don't have yours (FFR, making a self-contained reproducible example is super helpful), which is one way you could do this (I'm sure there are many more).
I created an intermediary lpurchase with lag(), and then used case_when() using is.na() as the logical predicate.
You don't actually need to separate out the mutate() calls (you can do multiple mutations inside of one mutate), I just wanted to give it a bit more visual clarity.
Oh I see, I thought as it was piped in from the group_by it would automatically do that, should I add this to my code to ensure this.
Really appreciate the help,
Sue