title: Putting rowwise counts of value occurences into new variables, how to do that in R with dplyr?
body: <p>I have a large dataframe (df) that looks like this:</p>
  
  <pre><code>structure(list(var1 = c(1, 2, 3, 4, 2, 3, 4, 3, 2), var2 = c(2, 
  3, 4, 1, 2, 1, 1, 1, 3), var3 = c(4, 4, 2, 3, 3, 1, 1, 1, 4), 
      var4 = c(2, 2, 2, 2, 3, 2, 3, 4, 1), var5 = c(4, 4, 2, 3, 
      3, 1, 1, 1, 4)), .Names = c("var1", "var2", "var3", "var4", 
  "var5"), row.names = c(NA, -9L), class = "data.frame")
  
    var1 var2 var3 var4 var5
  1    1    2    4    2    4
  2    2    3    4    2    4
  3    3    4    2    2    2
  4    4    1    3    2    3
  5    2    2    3    3    3
  6    3    1    1    2    1
  7    4    1    1    3    1
  8    3    1    1    4    1
  9    2    3    4    1    4
  </code></pre>
  
  <p>Now I need to count the occurence of values rowwise and make new variables of the counts. This should be the result:</p>
  
  <pre><code>  var1 var2 var3 var4 var5 n_1 n_2 n_3 n_4
  1    1    2    4    2    4   1   2   0   2
  2    2    3    4    2    4   0   2   1   2
  3    3    4    2    2    2   0   3   1   1
  4    4    1    3    2    3   1   1   2   1
  5    2    2    3    3    3   0   2   3   0
  6    3    1    1    2    1   3   1   1   0
  7    4    1    1    3    1   3   0   1   1
  8    3    1    1    4    1   3   0   1   1
  9    2    3    4    1    4   1   1   1   2
  </code></pre>
  
  <p>As you can see variable n_1 shows the rowcounts of the 1's, n_2 the row counts of the 2's, etc.</p>
  
  <p>I tried some dplyr functions (because I like their speed), but haven't succeeded yet. I know this is definately ugly code :-), but my approache would be something in this way:</p>
  
  <pre><code>newdf &lt;- mutate(rowwise(df, n_1 = sum(df==1))
  </code></pre>
  
  <p>Does anyone have an idea about how to deal with this problem?
  Many thanks in advance!</p>