title: Using case_when() within mutate_at() to recode several columns with different types of NA
body: <p>Given the data:</p>
  
  <pre><code>df &lt;- structure(list(cola = structure(c(5L, 9L, 6L, 2L, 7L, 10L, 3L, 
  8L, 1L, 4L), .Label = c("a", "b", "d", "g", "q", "r", "t", "w", 
  "x", "z"), class = "factor"), colb = c(156L, 8L, 6L, 100L, 49L, 
  31L, 189L, 77L, 154L, 171L), colc = c(0.207140279468149, 0.51990159181878, 
  0.402017514919862, 0.382948065642267, 0.488511856179684, 0.263168515404686, 
  0.38591041485779, 0.774066215148196, 0.763264901703224, 0.474355421960354
  ), cold = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("a", 
  "b"), class = "factor")), class = "data.frame", row.names = c(NA, 
  -10L))
  
  df
  #    cola colb      colc cold
  # 1     q  156 0.2071403    a
  # 2     x    8 0.5199016    b
  # 3     r    6 0.4020175    a
  # 4     b  100 0.3829481    b
  # 5     t   49 0.4885119    a
  # 6     z   31 0.2631685    b
  # 7     d  189 0.3859104    a
  # 8     w   77 0.7740662    b
  # 9     a  154 0.7632649    a
  # 10    g  171 0.4743554    b
  </code></pre>
  
  <p>If the value in <code>colc</code> in a particular row is <code>&gt;= 0.5</code>, I would like to replace the contents of all the other cells in that row with NA, except for the contents of <code>cold</code> for that row (which I would like to retain as it is).</p>
  
  <p>I attempted this with <code>dplyr::mutate_at()</code> and <code>base::ifelse()</code>, and it works fine:</p>
  
  <pre><code>df %&gt;% mutate_at(vars(-c(cold)), funs(ifelse(colc &gt;= 0.5, NA, .)))
  
  #    cola colb      colc cold
  # 1     5  156 0.2071403    a
  # 2    NA   NA        NA    b
  # 3     6    6 0.4020175    a
  # 4     2  100 0.3829481    b
  # 5     7   49 0.4885119    a
  # 6    10   31 0.2631685    b
  # 7     3  189 0.3859104    a
  # 8    NA   NA        NA    b
  # 9    NA   NA        NA    a
  # 10    4  171 0.4743554    b
  </code></pre>
  
  <p>But I would like to do this with <code>dplyr::case_when()</code>, as I might have more than one replacement condition to fulfill (e.g., replace with <code>"foo"</code> if <code>colc &lt; 0.5 &amp; colc &gt;= 0.3</code>. But <code>case_when()</code> does not appear to be playing nice:</p>
  
  <pre><code>df %&gt;% mutate_at(vars(-c(cold)), funs(case_when(colc &gt;= 0.5 ~ NA, TRUE ~ .)))
  </code></pre>
  
  <blockquote>
    <p>Error: must be a logical vector, not a factor object</p>
  </blockquote>
  
  <p>Why is this happening and what can I do to fix it? I assume this is because I am trying to convert multiple columns with different data types to NA. I tried to look for a solution online, but I wasn't able to find one.</p>
  
  <p><strong>Edit:</strong> in specific, I would like to preserve the data types of the various columns as they are.</p>