title: create matrix of z-scores in R
body: <p>I have a data.frame containing survey data on three binary variables. The data is already in a contingency table with the first 3 columns being answers (1=yes, 0 = no) and the fourth column showing the total number of answers. The rows is three different groups.  </p>
  
  <p>My aim is to calulate z-scores to check if the proportions are significantly different compared to the total</p>
  
  <p>this is my data:</p>
  
  <pre><code>library(dplyr) #loading libraries
  df &lt;- structure(list(var1 = c(416, 1300, 479, 417), 
                       var2 = c(265, 925,473, 279),
                       var3 = c(340, 1013, 344, 284),
                       totalN = c(1366, 4311,1904, 1233)),
                  class = "data.frame",
                  row.names = c(NA, -4L),
                  .Names = c("var1","var2", "var3", "totalN"))
  </code></pre>
  
  <p>and these are my total values</p>
  
  <pre><code>dfTotal &lt;-  df %&gt;% summarise_all(funs(sum(., na.rm=TRUE)))
  dfTotal
  dfTotal &lt;- data.frame(dfTotal)
  rownames(dfTotal) &lt;- "Total"
  </code></pre>
  
  <p>to calculate zScore I use the following formula:</p>
  
  <pre><code>zScore &lt;- function (cntA, totA, cntB, totB) {
    #calculate
    avgProportion &lt;- (cntA + cntB) / (totA + totB)
    probA &lt;- cntA/totA
    probB &lt;- cntB/totB
    SE &lt;- sqrt(avgProportion * (1-avgProportion)*(1/totA + 1/totB))
    zScore &lt;- (probA-probB) / SE
    return (zScore)
  }
  </code></pre>
  
  <p>is there a way using dplyr to calculate a 4x3 matrix that holds for all four groups and variables var1 to var3 the z-test-value against the total proportion?</p>
  
  <p>I am currently stuck with this bit of code:</p>
  
  <pre><code>df %&gt;% mutate_all(funs(zScore(., totalN,dftotal$var1,dfTotal$totalN)))
  </code></pre>
  
  <p>So the parameters currently used here as dftotal$var1 and dfTotal$totalN don't work, but I have no idea how to feed them into the formula. for the first parameter it must not be always var1 but should be var2, var3 (and totalN) to match the first parameter.</p>