I have a dataset with many different social media creators (creator_id). They posted many times (posting_count) and the posts are classified as ad if ad = 1. Now I always want to classify the 3 previous postings before ad = 1 as 1. Basically the "goal_variable" is what I want to get. A solution without a loop would be cool!!
creator_id <-c("aaa","aaa","aaa","aaa","aaa","aaa","aaa","aaa","bbb","bbb","bbb","bbb","bbb","bbb","bbb","bbb","bbb")
posting_count <- c(143,144,145,146,147,148,149,150,90,91,92,93,94,95,96,97,98)
ad <- c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1)
goal_variable <- c(0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,0)
df <- cbind(creator_id, posting_count, ad, goal_variable)