title: How do you replace missing values with 0 for cases meeting specific conditions in R?
body: <h2>Background:</h2>
  <p>I am working with a large dataset that contains longitudinal data on gambling behavior of 195,318 participants. The data is based on complete tracking of electronic gambling behavior within a gambling operator. Gambling behavior data is aggregated on a monthly level, a total of 70 months. I have an ID variable seperating participants, a time variable (months), as well as numerous gambling behavior variables such as active days played for given month, bets placed for given month, total losses for given month, etc. Participants vary in when they've been active gambling. One participant may have gambled at month 2, 3, 4, and 7, another participant at 3, 5, and 7, and a third at 23, 24, 48, 65 etc. As such, there are considerable amounts of &quot;missing values&quot;. However, because every instance of gambling is tracked, missing in this data set means that the person did not gamble. In other words, missing equals 0.</p>
  <h2>Problem/goal:</h2>
  <p>I want to impute 0 for missing values (&quot;NA&quot;). <strong>However</strong> I only want to do so under specific circumstances. Specifically I want to impute 0 for missing values within what I define as a participant's &quot;active period&quot; and leave everything else as is.</p>
  <p>A participant's active period is every month between their first active month gambling and their last month gambling. For example, for a participant that gambled at month 2, 3, 4, and 7 I want to impute 0 at month 5 and 6. Every other month, i.e. 1 and 9 to 70, I want to stay as NA. I am struggling to write code that achieves this. I'm new to R.</p>
  <h2>Example data frame and code</h2>
  <p>Below is example code that produces a data frame that illustrates key characteristics described in my problem. In this code there's only 2 participants, 1 gambling behavior variable and 10 time points (&quot;waves&quot;). I've included a data frame in &quot;long format&quot; and &quot;wide format&quot; because I'm unsure which one would would be most helpful/informative. A time variable is included in the &quot;long format&quot;. My actual data set is in long format, but I am familiar with how to switch between the two.</p>
  <pre class="lang-r prettyprint-override"><code># Example variables and data frame in long form
    # Includes id variable, time variable and example variable
  id &lt;- c(1, 1, 1, 1, 2, 2, 2)
  time &lt;- c(2, 3, 4, 7, 3, 5, 7)
  daysPlayed &lt;- c(2, 2, 3, 3, 2, 2, 2)
  dfLong &lt;- data.frame(id = id, time = time, daysPlayed = daysPlayed)
  </code></pre>
  <p><sup>Created on 2020-03-28 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>
  <pre class="lang-r prettyprint-override"><code># Example variables and data frame in wide form
    # Includes id variable, days played in given month 
  id &lt;- c(1, 2)
  daysPlayed.1 &lt;- c(NA, NA)
  daysPlayed.2 &lt;- c(2, NA)
  daysPlayed.3 &lt;- c(2, 2)
  daysPlayed.4 &lt;- c(3, NA)
  daysPlayed.5 &lt;- c(NA, 2)
  daysPlayed.6 &lt;- c(NA, NA)
  daysPlayed.7 &lt;- c(3, 2)
  daysPlayed.8 &lt;- c(NA, NA)
  daysPlayed.9 &lt;- c(NA, NA)
  daysPlayed.10 &lt;- c(NA, NA)
  dfWide &lt;- data.frame(id=id, daysPlayed.1 = daysPlayed.1, daysPlayed.2 = daysPlayed.2,
                   daysPlayed.3 = daysPlayed.3, daysPlayed.4 = daysPlayed.4,
                   daysPlayed.5 = daysPlayed.5, daysPlayed.6 = daysPlayed.6,
                   daysPlayed.7 = daysPlayed.7, daysPlayed.8 = daysPlayed.8,
                   daysPlayed.9 = daysPlayed.9, daysPlayed.10 = daysPlayed.10)
  </code></pre>
  <p><sup>Created on 2020-03-28 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>