Hi, Excuse me for a so dummy question, but I really can't manage to do this very simple thing : I have a data frame, and I would like to calculate two vectors which contain the sums respectively of the rows and of the columns of the matrix. There is surely a very simple way to do that, but I didn't find it... Many thanks in advance. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Julien Barnier wrote:> > Hi, > > Excuse me for a so dummy question, but I really can't manage to do this > very simple thing : I have a data frame, and I would like to calculate > two vectors which contain the sums respectively of the rows and of the > columns of the matrix. > > There is surely a very simple way to do that, but I didn't find it...In R <= 1.4.1: apply(x, 1, sum) apply(x, 2, sum) In R >= 1.5.0 (in development): colSums(x) rowSums(x) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 24 Apr 2002, Julien Barnier wrote:> Excuse me for a so dummy question, but I really can't manage to do this > very simple thing : I have a data frame, and I would like to calculate > two vectors which contain the sums respectively of the rows and of the > columns of the matrix.A data frame or a matrix? You can only do this for data frames all of whose columns are numeric, or for numeric matrices. Suppose A is either sort of object: apply(A, 1, sum) # row sums apply(A, 2, sum) # columns sums As from next week's R 1.5.0 you will be able to use rowSums(A) and colSums(A).> There is surely a very simple way to do that, but I didn't find it...Not `very simple' until next week. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Julien, Assuming that the data frame contains only numeric data, you can use apply: apply(data, 1, sum) will give you the row sums, and apply(data, 2, sum) the column sums. John At 09:49 AM 4/24/2002 +0200, Julien Barnier wrote:>Excuse me for a so dummy question, but I really can't manage to do this >very simple thing : I have a data frame, and I would like to calculate >two vectors which contain the sums respectively of the rows and of the >columns of the matrix.----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._