In this post, we will see how to resolve Create dummies based on a window of observations (in R or Stata)
Question:
For example, if a given month is the second observation for a given id, I want the dummy to equal 0 because there are not three observations for this id before it.
I may have potentially more than a single “1” for each id if there is more than one month fitting into a window of 7 observations.
The expected output is in a column named “desired_output”.
My real window consists of 25 months (12 after and 12 before), but I use 7 so that the example dataset is not too big.
Stata example:
Best Answer:
If it’s just observations a window within each group means “get X first and last rows”.dplyr
< 1.1.0If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com