title: Is it possible to use custom function in dplyr mutate with the option to use na.rm = TRUE or na.rm = FALSE
body: <p>I have a function to scale/ normalize / z-score transform a number of variables using mutate_at. The source of the function is in the link:
  <a href="https://dplyr.tidyverse.org/reference/mutate_all.html" rel="nofollow noreferrer">https://dplyr.tidyverse.org/reference/mutate_all.html</a></p>
  
  <p><code>scale &lt;- function(x, na.rm = FALSE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm)</code></p>
  
  <p>Use of that function results in all NAs if there are any NAs present in the initial variable, as outlined in the example below:</p>
  
  <pre><code>#make df1
  set.seed(123)
  df &lt;- data.frame(
    col_A = c(5, NA,2,4, 4,5,8,3,7,9),
    col_B = as.numeric(sample(20:90, size = 10)),
    col_C = as.numeric(sample(1000:2000, size = 10))
  )
  
  df
  </code></pre>
  
  <p>I have tried setting na.rm = TRUE, which seems to achieve what I'm after.</p>
  
  <p><code>scale_narm_true &lt;- function(x, na.rm = TRUE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm)
  </code></p>
  
  <pre><code>vars &lt;- c("col_A", "col_B")
  df_z_score &lt;- df %&gt;%
    mutate_at(vars, list(scaled_var = scale)) %&gt;% # introduces NAs in the resulting variables
    mutate_at(vars, list(scaled_narm_true_var = scale_narm_true)) # works as expected and desired
  </code></pre>
  
  <p>However, what I'm really after is the option to include na.rm = TRUE in the actual mutate_at call, such as below</p>
  
  <pre><code>df_z_score_attempt &lt;- df %&gt;%
    mutate_at(vars, list(scaled_var = scale, na.rm=T)) # this doesn't work!
  </code></pre>
  
  <p>Any help would be appreciated, especially since it's supposed to be possible according to <a href="https://dplyr.tidyverse.org/reference/mutate_all.html" rel="nofollow noreferrer">https://dplyr.tidyverse.org/reference/mutate_all.html</a>, stating that this is possible:</p>
  
  <pre><code>starwars %&gt;% mutate_at(c("height", "mass"), scale2, na.rm = TRUE)
  </code></pre>