title: use of rowMeans, rowSums, colMeans, colSums
body: <p>I am working with a matrix and the commands rowSums and rowMeans, colSums and colMeans. I have most of it working until I want to take the sum of rowSums, sum of colSums, mean of rowMeans, mean of colMeans. Please see last line of code below. rbind(matRow, colSums, colMeans).  Can you assist in correcting the lower 2x2 matrix in the matrix mat?  Commands such as rowSums[colSums] are producing errors.</p>
  
  <h1>Input table of data</h1>
  
  <pre><code>mydata=read.table("mydata.txt")
   mydata
    V1 V2 V3 V4
  1  1  2  4  7
  2  2  4  6  8
  </code></pre>
  
  <h1>convert table to matrix in R</h1>
  
  <pre><code>mat=as.matrix(mydata)
  mat
       V1 V2 V3 V4
  [1,]  1  2  4  7
  [2,]  2  4  6  8
  </code></pre>
  
  <h1>Add row and column names</h1>
  
  <pre><code>rownames(mat) &lt;- c("r1", "r2")
  colnames(mat) &lt;- c("c1","c2","c3","c4")
  mat
     c1 c2 c3 c4
  r1  1  2  4  7
  r2  2  4  6  8
  </code></pre>
  
  <h1>Vital Statistics</h1>
  
  <pre><code>dim(mat)
  [1] 2 4
  rowSums=rowSums(mat)
  colSums=colSums(mat)
  rowMeans=rowMeans(mat)
  colMeans=colMeans(mat)
  </code></pre>
  
  <h1>Append rowSums to right of mat</h1>
  
  <pre><code>matRow=cbind(mat,rowSums,rowMeans)
  matRow
     c1 c2 c3 c4 rowSums rowMeans
  r1  1  2  4  7      14      3.5
  r2  2  4  6  8      20      5.0
  
  #Append colSums and colMeans below matRow
  #far lower R corner 2x2 is not correct
  #see final computation
  matCol=rbind(matRow,colSums,colMeans)
  Warning message:
  In rbind(matRow, colSums, colMeans) :
    number of columns of result is not a multiple of vector length (arg 2)
  matCol
            c1 c2 c3   c4 rowSums rowMeans
  r1       1.0  2  4  7.0    14.0      3.5
  r2       2.0  4  6  8.0    20.0      5.0
  colSums  3.0  6 10 15.0     3.0      6.0
  colMeans 1.5  3  5  7.5     1.5      3.0
  </code></pre>