title: trouble creating a pre-event indicator variable
body: <p>i am trying to make a variable to indicate whether or not a given year is the year just before a certain event, but i am having some trouble.</p>
  <p>this is what my initial data looks like.</p>
  <pre><code>&gt; dat
  # A tibble: 12 x 3
     country  year event
     &lt;chr&gt;   &lt;dbl&gt; &lt;dbl&gt;
   1 a        2000     0
   2 a        2001     0
   3 a        2002     1
   4 a        2003     1
   5 a        2004     1
   6 a        2005     1
   7 b        2000     0
   8 b        2001     0
   9 b        2002     0
  10 b        2003     0
  11 b        2004     1
  12 b        2005     1
  </code></pre>
  <p>since the event starts in 2002 for country <code>a</code> and 2004 for country <code>b</code>, i would want a new variable <code>pre_event</code> to have a value of <code>1</code> in 2001 and 2003, but have a value of <code>0</code> in all other years.</p>
  <p>so far i only seem to be able to do part of this successfully.</p>
  <pre><code>&gt; dat %&gt;%
  +   group_by(country) %&gt;%
  +   arrange(country, year) %&gt;%
  +   mutate(pre_event = lead(event))
  # A tibble: 12 x 4
  # Groups:   country [2]
     country  year event pre_event
     &lt;chr&gt;   &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;
   1 a        2000     0         0
   2 a        2001     0         1
   3 a        2002     1         1
   4 a        2003     1         1
   5 a        2004     1         1
   6 a        2005     1        NA
   7 b        2000     0         0
   8 b        2001     0         0
   9 b        2002     0         0
  10 b        2003     0         1
  11 b        2004     1         1
  12 b        2005     1        NA
  </code></pre>
  <p>but i want my data to look like this. what should i do?</p>
  <pre><code>   country  year event pre_event
     &lt;chr&gt;   &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;
   1 a        2000     0         0
   2 a        2001     0         1
   3 a        2002     1         0
   4 a        2003     1         0
   5 a        2004     1         0
   6 a        2005     1         0
   7 b        2000     0         0
   8 b        2001     0         0
   9 b        2002     0         0
  10 b        2003     0         1
  11 b        2004     1         0
  12 b        2005     1         0
  </code></pre>
  <p>edit:</p>
  <p>adding real snapshot of data:</p>
  <pre><code>structure(list(country_name = c(&quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, 
  &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, 
  &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, 
  &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, 
  &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, &quot;Guinea-Bissau&quot;, 
  &quot;Guinea-Bissau&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, 
  &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, 
  &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, &quot;Gambia&quot;, 
  &quot;Mali&quot;, &quot;Mali&quot;), year = c(1981, 1982, 1983, 1984, 1985, 1986, 
  1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 
  1998, 1999, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 
  1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 1981, 
  1982), internalconflict = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0), gdpgrowth = c(0.21656057, 0.10471205, 
  -0.042654045, 0.034653414, 0.036682621, -0.035384621, 0, 0.043062218, 
  0.067278333, -0.012893987, -0.13933238, 0.06913998, -0.0011880503, 
  0.0085358936, 0.019053213, 0.021861792, 0.031022603, -0.29643786, 
  0.056630101, -0.12586033, -0.019122632, -0.1731651, 0.11095698, 
  -0.039950069, 0.096228868, 0.011862385, -0.057444274, 0.0049751345, 
  -0.011138618, -0.013032082, -0.007978335, -0.008393826, -0.032121219, 
  -0.022602938, -0.0088469042, 0.017218538, 0.018114341, 0.034760576, 
  -0.03759395, 0.023437429)), row.names = c(NA, -40L), groups = structure(list(
      country_name = c(&quot;Gambia&quot;, &quot;Guinea-Bissau&quot;, &quot;Mali&quot;), .rows = structure(list(
          20:38, 1:19, 39:40), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
      &quot;vctrs_vctr&quot;, &quot;list&quot;))), row.names = c(NA, -3L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;), .drop = TRUE), class = c(&quot;grouped_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>