I have the following dataframe with three columns - FIRM, YEAR and DUMMY (0,1). For every FIRM, I wanted to scan all years and identify the first case where the DUMMY value of 1 is repeated more than once (in consecutive rows). Then, I want to create a new column which contains 0, for all the years in which the DUMMY was 1 and contains -1,-2,-3 for the years before it, and 1,2,3 for the years after it.
------------------------------
| FIRM | YEAR | DUMMY| NEW_COL
------------------------------
| A | 2006 | 0 | 0 |
------------------------------
| A | 2007 | 1 | 0 |
------------------------------
| A | 2008 | 0 | 0 |
------------------------------
| B | 2006 | 0 | 0 |
------------------------------
| B | 2007 | 0 | -1 |
------------------------------
| B | 2008 | 1 | 0 |
------------------------------
| B | 2009 | 1 | 0 |
------------------------------
| B | 2010 | 0 | 1 |
------------------------------
| B | 2011 | 0 | 2 |
------------------------------
| B | 2012 | 1 | 3 |
------------------------------
| B | 2013 | 1 | 4 |
------------------------------