Hi,
I have a dataset arranged as event-month i.e., each row represents a month. There are following variables in the dataset:
I want to calculate the total number of events in say last six months from each event. For example, for the observation on bold (row values 1931 - 3 - 4 - 0), the number of events in last six months are two (1+1).
An automated code for this calculation would help me a lot. Thanks in advance.
I have a dataset arranged as event-month i.e., each row represents a month. There are following variables in the dataset:
- event variable is a count of events i.e., (min. 0 (no event in that particular month), max. 17 (many events in the same month)).
- timebetween variable is a incremental count of number of months elapsed since the last event occurred i.e. time between two non-zero values of the event variable. Example: say first event(s) occurred in January and the second event(s) occurred in June then the timebetween these events is counted as 5 months.
- Year variable corresponding to the year in which the event(s) occurred.
- Month variable corresponding to the month in which the event(s) occurred.
year | month | event | timebetween |
1930 | 1 | 0 | 10 |
1930 | 2 | 0 | 11 |
1930 | 3 | 0 | 12 |
1930 | 4 | 0 | 13 |
1930 | 5 | 0 | 14 |
1930 | 6 | 0 | 15 |
1930 | 7 | 0 | 16 |
1930 | 8 | 1 | 0 |
1930 | 9 | 0 | 1 |
1930 | 10 | 0 | 2 |
1930 | 11 | 0 | 3 |
1930 | 12 | 0 | 4 |
1931 | 1 | 1 | 0 |
1931 | 2 | 1 | 0 |
1931 | 3 | 4 | 0 |
1931 | 4 | 0 | 1 |
1931 | 5 | 2 | 0 |
1931 | 6 | 0 | 1 |
1931 | 7 | 0 | 2 |
1931 | 8 | 0 | 3 |
1931 | 9 | 0 | 4 |
1931 | 10 | 0 | 5 |
1931 | 11 | 0 | 6 |
1931 | 12 | 0 | 7 |
1932 | 1 | 0 | 8 |
1932 | 2 | 2 | 0 |
1932 | 3 | 0 | 1 |
1932 | 4 | 1 | 0 |
1932 | 5 | 1 | 0 |
1932 | 6 | 0 | 1 |
I want to calculate the total number of events in say last six months from each event. For example, for the observation on bold (row values 1931 - 3 - 4 - 0), the number of events in last six months are two (1+1).
An automated code for this calculation would help me a lot. Thanks in advance.
Comment