I see you wanted an answer with for-loops. Here's one:
months_since_last_purchase <- function(df) {
df$recency <- NA # create an empty vector to store recency
months_since = 0 # initialise our months since counter to zero
for(row in 1:nrow(df)){ # loop through our rows
if(df$purchases[row] == 0){ # if we did not purchase something this month
months_since = months_since + 1 # increment months_since
df$recency[row] <- months_since # set the recency to months since
} else { # else if we did purchase something this month
months_since = months_since + 1 # increment months_since
if(months_since == 1){ # and if we purchased something last month as well
df$recency[row] = NA # set the recency to NA
}else{ # else we didn't purchase something last month
df$recency[row] <- months_since # set the recency to the months_since
}
months_since = 0 # reset the months since to zero
}
}
df # return the modified dataframe
}
To use this function we just created, on your df, use something like this:
new_df <- months_since_last_purchase(df)
If I plan to reuse this function I will save it somewhere such as a directory called scripts and to reuse it I would use:
source("scripts/months_since_last_purchase.R")
Output:
id month purchases recency
1 1 1 3 NA
2 1 2 0 1
3 1 3 0 2
4 1 4 1 3
5 2 1 1 NA
6 2 2 0 1
7 2 3 3 2
8 2 4 1 NA
R often frowns on for-loops as vector operations are faster and more elegant, but I still find for-loops handy when speed is not important.