title: rowSums excluding a particular value in a dplyr pipe without modifying the underlying data frame
body: <p>I have a data frame of questionnaire data in wide format, with each column representing a questionnaire item.</p>
  
  <p>The data looks something like the following:</p>
  
  <pre><code>df &lt;- data.frame(Q1 = c(1, 4, 2, 3, 1, 1, 4, 4, 1, 2), 
               Q2 = c(NA, 3, 1, 4, NA, NA, 3, 4, 1, 2),
               Q3 = c(3, 4, 1, 2, 4, NA, NA, 1, 1, 2),
               Q4 = c(NA, 4, 1, 1, 1, 3, NA, 2, 2, NA))
  </code></pre>
  
  <p>I want to use the <code>rowSums</code> function to sum up the values in each row that are not "4" and to exclude the NAs and divide the result by the number of non-4 and non-NA columns (using a dplyr pipe). I do not want to replace the 4s in the underlying data frame; I want to leave it as it is.</p>
  
  <p>As I do not know how to divide the result by the number of non-4 and non-NA columns, I have only tried attempting the first part of my question. I have used the following codes to attempt the first part, but it did not work:</p>
  
  <pre><code>library(dplyr)
  
  df &lt;- df %&gt;%
    as.data.frame() %&gt;%
    mutate(sum = rowSums(.[. != 4, ], na.rm = TRUE))
  </code></pre>
  
  <p>The desired output would look something like the following:
  <a href="https://i.stack.imgur.com/MTJDn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MTJDn.png" alt="rowSums excluding particular value"></a></p>
  
  <p>In the screenshot above, the "mean" column is the sum of non-4 and non-NA values divided by the number of non-4 and non-NA columns.</p>
  
  <p>Thanks!</p>