title: Mean imputation by group
body: <p>I want to replace the NAs with the mean of columns for each country with the following code, but I receive the following error message. Any guidance is much appreciated.</p>
  <pre><code>mydata &lt;- mydata %&gt;% group_by(country) %&gt;% 
  mutate(across(c(battles, explosions, protests, riots, violenceac), ~ replace_na(.x, 
  mean(.x, na.rm = TRUE)))) %&gt;%
  ungroup()
  </code></pre>
  <p>Error in <code>mutate()</code>:
  ! Problem while computing <code>..1 = across(...)</code>.
  ℹ The error occurred in group 1: country = &quot;Afghanistan&quot;.
  Caused by error in <code>across()</code>:
  ! Problem while computing column <code>battles</code>.
  Caused by error in <code>vec_assign()</code>:
  ! Can't convert from <code>replace</code>  to <code>data</code>  due to loss of precision.
  • Locations: 1</p>
  <pre><code>head(mydata,3)
   X month year       date     country code                     region              
   income battles
  1:  17   Jan 2018 20.01.2018 Afghanistan  AFG                 South Asia          Low 
  income     777
  2:  66   Jan 2018 20.01.2018     Albania  ALB      Europe &amp; Central Asia Upper middle 
  income      NA
  3: 114   Jan 2018 20.01.2018     Algeria  DZA Middle East &amp; North Africa Lower middle 
  income       4
  explosions protests riots violenceac   vaa polstab goveff   rol total_cases 
  total_deaths stringency
  1:        281        9     2         38 -1,08   -2,73  -1,52 -1,81        &lt;NA&gt;         
  &lt;NA&gt;       &lt;NA&gt;
  2:         NA        5    NA         NA  0,09    0,08  -0,14 -0,36        &lt;NA&gt;         
  &lt;NA&gt;       &lt;NA&gt;
  3:         NA       56    13          1  -1,1   -0,86  -0,53 -0,78        &lt;NA&gt;         
  </code></pre>
  <p>       </p>