title: Add a new column to a tibble and use case_when() with multiple types of replacement values in RHS
body: <p>My repex toy data:</p>
  <pre><code>(df &lt;- tibble(
    symp_A = c(&quot;Yes&quot;, NA, NA, &quot;Yes&quot;, &quot;No&quot;, NA), 
    symp_B = c(&quot;Yes&quot;, NA, NA, &quot;No&quot;, &quot;No&quot;, NA), 
    symp_C = c(&quot;Yes&quot;, NA, NA, &quot;No&quot;, &quot;No&quot;, &quot;No&quot;), 
    symp_D = c(&quot;No&quot;, NA, NA, &quot;No&quot;, &quot;No&quot;, &quot;No&quot;), 
    age = c(&quot;56&quot;, &quot;74&quot;, &quot;58&quot;, NA, &quot;36&quot;, &quot;63&quot;)))
  
    symp_A symp_B symp_C symp_D age  
    &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;
  1 Yes    Yes    Yes    No     56   
  2 NA     NA     NA     NA     74   
  3 NA     NA     NA     NA     58   
  4 Yes    No     No     No     NA   
  5 No     No     No     No     36   
  6 NA     NA     No     No     63
  </code></pre>
  <p>I'm looking for a (preferably a <code>tidy</code> version) solution, that does the following:</p>
  <ol>
  <li>Create a new column <code>col_count</code> with the following conditions:
  <ol>
  <li>go across columns that start with &quot;symp&quot; using <code>dplyr::start_with()</code></li>
  <li>if a row has a <code>Yes</code> then place the count of total <code>Yes</code>s in <code>col_count</code></li>
  <li>if a row has only <code>No</code>, OR <code>No</code> + <code>NA</code>s, then place <code>0</code></li>
  <li>if a row is all <code>NA</code> then place <code>NA</code></li>
  </ol>
  </li>
  </ol>
  <p>so the desired output would look like this:</p>
  <pre><code># A tibble: 6 x 6
    symp_A symp_B symp_C symp_D age   count_col
    &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;     &lt;dbl&gt;
  1 Yes    Yes    Yes    No     56            3
  2 NA     NA     NA     NA     74           NA
  3 NA     NA     NA     NA     58           NA
  4 Yes    No     No     No     NA            1
  5 No     No     No     No     36            0
  6 NA     NA     No     No     63            0
  </code></pre>
  <p>My attempt:</p>
  <pre><code>df %&gt;% dplyr::mutate(count_col = purrr::pmap_dbl(#new col, map to
    dplyr::select(., starts_with(&quot;symp&quot;)), ~ #cols that starts with symp
      dplyr::case_when( #define conditions
        any(c(...) == &quot;Yes&quot;) ~ {rowSums(.==&quot;Yes&quot;, na.rm=TRUE)}, #if any Yes, tally Yes
        any(c(...) == &quot;No&quot;) ~ 0,#not overwriting first condition, tally No
      TRUE ~ NA_real_))) #catch all NA cases and place NA
  </code></pre>
  <p>errors:</p>
  <pre><code>Error: Problem with `mutate()` input `count_col`.
  x 'x' must be an array of at least two dimensions
  ℹ Input `count_col` is `purrr::pmap_dbl(...)`.  
  </code></pre>
  <p>I can get the desired output using a not so clean long way:</p>
  <pre><code>df$yes_count &lt;- rowSums(select(df, tidyselect::starts_with(&quot;symp&quot;))==&quot;Yes&quot;, na.rm=TRUE) #temp yes sum column
  df$no_count &lt;- rowSums(select(df, tidyselect::starts_with(&quot;symp&quot;))==&quot;No&quot;, na.rm=TRUE) #temp no sum column
  
  df %&gt;% 
    dplyr::mutate(count_col = #new column
                    dplyr::case_when( #conditions:
                      yes_count &gt; 0 ~ yes_count, #if any are Yes, then use the sum
                      no_count &gt; 0 ~ 0, #if any is No (with or without NA), use &quot;0&quot;. Won't overwrite first conditions 
                      TRUE ~ NA_real_)) %&gt;% #everything else (all NA), put NA
    dplyr::select(-yes_count, -no_count) #remove temp columns
  </code></pre>
  <p>But I'd really like to know how to get my first approach to work. Admittedly the <code>map_</code> business is very new to me so my guess is that my RHS conditions in <code>case_when</code> probably are breaking a requirements.</p>