title: How to replace all <NA> values in a data.frame using forcats::fct_explicit_na()?
body: <p>I have a data frame with 19 variables, 17 of which are factors. Some of these factors contain missing values, coded as NA. I would like to recode missings as a separate factor level "to_impute" using forcats::fct_explicit_na() for all factors in the data frame.</p>
  
  <p>A small example with two factor variables:</p>
  
  <pre><code>df &lt;- structure(list(loc_len = structure(c(NA, NA, NA, NA, NA, NA, 
  1L, 1L, 3L, 1L), .Label = c("No", "&lt; 5 sec", "5 sec - &lt; 1 min", 
  "1 - 5 min", "&gt; 5 min", "Unknown duration"), class = "factor"), 
      AMS = structure(c(1L, 2L, NA, 1L, 1L, NA, NA, NA, NA, NA), .Label = c("No", 
      "Yes"), class = "factor")), .Names = c("loc_len", "AMS"), row.names = c(NA, 
  -10L), class = c("tbl_df", "tbl", "data.frame"))
  
  table(df$loc_len, useNA = "always")
  
                No          &lt; 5 sec  5 sec - &lt; 1 min        1 - 5 min          &gt; 5 min Unknown duration             &lt;NA&gt; 
                 3                0                1                0                0                0                6 
  </code></pre>
  
  <p>The code below does this for two variables.  I'd like to do this for all factor variables 'f_names' in the data frame.  Is there a way to 'vectorize' fct_explicit_na()?</p>
  
  <pre><code>f_names &lt;- names(Filter(is.factor, df))
  
   f_names
  [1] "loc_len" "AMS"
  </code></pre>
  
  <p>The code below does what I want, but separately for each factor:</p>
  
  <pre><code>df_new &lt;- df  %&gt;% 
                      mutate(loc_len = fct_explicit_na(loc_len, na_level = "to_impute")) %&gt;% 
                      mutate(AMS = fct_explicit_na(AMS, na_level = "to_impute"))
  </code></pre>
  
  <p>I'd like tables of this sort for all factors in the dataset, names in 'f_names' :</p>
  
  <pre><code>lapply(df_new, function(x) data.frame(table(x, useNA = "always")))
  </code></pre>
  
  <p>Now is:</p>
  
  <pre><code>$loc_len
                   x Freq
  1               No    3
  2          &lt; 5 sec    0
  3  5 sec - &lt; 1 min    1
  4        1 - 5 min    0
  5          &gt; 5 min    0
  6 Unknown duration    0
  7        to_impute    6
  8             &lt;NA&gt;    0
  
  $AMS
            x Freq
  1        No    3
  2       Yes    1
  3 to_impute    6
  4      &lt;NA&gt;    0
  </code></pre>