• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Create dummies based on a window of observations (in R or Stata)

Resolved: Create dummies based on a window of observations (in R or Stata)

0
By Isaac Tonny on 15/03/2023 Issue
Share
Facebook Twitter LinkedIn

In this post, we will see how to resolve Create dummies based on a window of observations (in R or Stata)

Question:

For each id, I would like to create a dummy that equals 1 if a given month is in a window of 7 observations (three before and three after).
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.0

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

r stata
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Resolved: Sort dataframe columns value by close it to another column

27/03/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.