title: Replace NA with 0 depending on group (rows) and variable names (column)
body: <p>I have a large data set and want to replace many NAs, but not all.</p>
  
  <p>In one group i want to replace all NAs with 0.
  In the other group i want to replace all NAs with 0, but only in variables that do not include a certain part of the variable name e.g. 'b'</p>
  
  <p>Here is an example:</p>
  
  <pre><code>group &lt;- c(1,1,2,2,2)
  abc &lt;- c(1,NA,NA,NA,NA)
  bcd &lt;- c(2,1,NA,NA,NA)
  cde &lt;- c(5,NA,NA,1,2)
  df &lt;- data.frame(group,abc,bcd,cde)
  
    group abc bcd cde
  1     1   1   2   5
  2     1  NA   1  NA
  3     2  NA  NA  NA
  4     2  NA  NA   1
  5     2  NA  NA   2
  </code></pre>
  
  <p>This is what i want:</p>
  
  <pre><code>  group abc bcd cde
  1     1   1   2   5
  2     1   0   1   0
  3     2  NA  NA   0
  4     2  NA  NA   1
  5     2  NA  NA   2
  </code></pre>
  
  <p>This is what i tried:</p>
  
  <pre><code>#set 0 in first group: this works fine
  df[is.na(df) &amp; df$group==1] &lt;- 0
  #set 0 in second group but only if the variable name includes b: does not work
  df[is.na(df) &amp; df$group==2 &amp; !grepl('b',colnames(df))] &lt;- 0
  </code></pre>
  
  <p>dplyr solutions are welcome as well as basic</p>