title: replace NA in a dplyr chain
body: <p><strong>Question has been edited from the original</strong>.</p>
  
  <p>After reading this interesting <a href="https://stackoverflow.com/questions/21701537/does-mutate-change-tbl-by-reference">discussion</a> I was wondering how to replace NAs in a column using dplyr in, for example, the Lahman batting data:</p>
  
  <pre><code>Source: local data frame [96,600 x 3]
  Groups: teamID
  
     yearID teamID G_batting
  1    2004    SFN        11
  2    2006    CHN        43
  3    2007    CHA         2
  4    2008    BOS         5
  5    2009    SEA         3
  6    2010    SEA         4
  7    2012    NYA        NA
  </code></pre>
  
  <p>The following does <strong>not</strong> work as I expected</p>
  
  <pre><code>library(dplyr)
  library(Lahman)
  
  df &lt;- Batting[ c("yearID", "teamID", "G_batting") ]
  df &lt;- group_by(df, teamID )
  df$G_batting[is.na(df$G_batting)] &lt;- mean(df$G_batting, na.rm = TRUE)
  </code></pre>
  
  <p>Source: local data frame [20 x 3]
  Groups: yearID, teamID</p>
  
  <pre><code>   yearID teamID G_batting
  1    2004    SFN  11.00000
  2    2006    CHN  43.00000
  3    2007    CHA   2.00000
  4    2008    BOS   5.00000
  5    2009    SEA   3.00000
  6    2010    SEA   4.00000
  7    2012    NYA  **49.07894**
  
  &gt; mean(Batting$G_battin, na.rm = TRUE)
  [1] **49.07894**
  </code></pre>
  
  <p>In fact it imputed the overall mean and not the group mean. How would you do this in a dplyr chain? Using <code>transform</code> from base R also does <strong>not</strong> work as it imputed the overall mean and not the group mean. Also this approach converts the data to a regular dat. a frame. Is there a better way to do this?</p>
  
  <pre><code>df %.% 
    group_by( yearID ) %.%
    transform(G_batting = ifelse(is.na(G_batting), 
      mean(G_batting, na.rm = TRUE), 
      G_batting)
    )
  </code></pre>
  
  <p>Edit: Replacing <code>transform</code> with <code>mutate</code> gives the following error</p>
  
  <pre><code>Error in mutate_impl(.data, named_dots(...), environment()) : 
    INTEGER() can only be applied to a 'integer', not a 'double'
  </code></pre>
  
  <p>Edit: Adding as.integer seems to resolve the error and <strong>does</strong> produce the expected result. See also @eddi's answer.</p>
  
  <pre><code>df %.% 
    group_by( teamID ) %.%
    mutate(G_batting = ifelse(is.na(G_batting), as.integer(mean(G_batting, na.rm = TRUE)), G_batting))
  
  Source: local data frame [96,600 x 3]
  Groups: teamID
  
     yearID teamID G_batting
  1    2004    SFN        11
  2    2006    CHN        43
  3    2007    CHA         2
  4    2008    BOS         5
  5    2009    SEA         3
  6    2010    SEA         4
  7    2012    NYA        47
  
  &gt; mean_NYA &lt;- mean(filter(df, teamID == "NYA")$G_batting, na.rm = TRUE)
  &gt; as.integer(mean_NYA)
  [1] 47
  </code></pre>
  
  <p>Edit: Following up on @Romain's comment I installed dplyr from github:</p>
  
  <pre><code>&gt; head(df,10)
     yearID teamID G_batting
  1    2004    SFN        11
  2    2006    CHN        43
  3    2007    CHA         2
  4    2008    BOS         5
  5    2009    SEA         3
  6    2010    SEA         4
  7    2012    NYA        NA
  8    1954    ML1       122
  9    1955    ML1       153
  10   1956    ML1       153
  
  &gt; df %.% 
  +   group_by(teamID)  %.%
  +   mutate(G_batting = ifelse(is.na(G_batting), mean(G_batting, na.rm = TRUE), G_batting))
  Source: local data frame [96,600 x 3]
  Groups: teamID
  
     yearID teamID  G_batting
  1    2004    SFN          0
  2    2006    CHN          0
  3    2007    CHA          0
  4    2008    BOS          0
  5    2009    SEA          0
  6    2010    SEA 1074266112
  7    2012    NYA   90693125
  8    1954    ML1        122
  9    1955    ML1        153
  10   1956    ML1        153
  ..    ...    ...        ...
  </code></pre>
  
  <p>So I didn't get the error (good) but I got a (seemingly) strange result.</p>