Does anyone know how to get a vector of column sum from a data frame? You can use colSums(), but this gives you a object of type "numeric" with the column labels in the first row, and the sums in the second row. I just want a vector of the sums, and I can't figure out a way to index the "numeric" object. Thanks!
Gabor Csardi
2008-Feb-29 17:10 UTC
[R] Column sums from a data frame (without the headers)
colSums gives you exactly what you want, but the vector is *named*. You can use it like a not-named numeric vector, without much trouble. If you still want to get rid of the names see ?unname G. On Fri, Feb 29, 2008 at 12:02:46PM -0500, Jason Horn wrote:> Does anyone know how to get a vector of column sum from a data frame? > You can use colSums(), but this gives you a object of type "numeric" > with the column labels in the first row, and the sums in the second > row. I just want a vector of the sums, and I can't figure out a way > to index the "numeric" object. > > Thanks! > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Erik Iverson
2008-Feb-29 17:14 UTC
[R] Column sums from a data frame (without the headers)
Jason - colSums does return an object of class "numeric", which is certainly a vector. The vector it returns happens to have names. If you want an unnamed vector of the sums, just set the names attribute to NULL. This will remove the names. However, you say you can't figure out how to index the object that colSums returns. You index it just like any other vector, but you can use names in addition. ## ALL UNTESTED! ## test data.frame testdf <- data.frame(a = rnorm(10), b = rnorm(10)) cs <- colSums(testdf) class(cs) ## numeric is.vector(cs) ## TRUE names(cs) ## look at the names of the vector cs[1] ## first element of cs cs["a"] ## same as above ## get rid of names attribute names(cs) <- NULL cs ## look at cs, no more names cs[1] ## still works cs["a"] ## no longer works Best, Erik Iverson Jason Horn wrote:> Does anyone know how to get a vector of column sum from a data frame? > You can use colSums(), but this gives you a object of type "numeric" > with the column labels in the first row, and the sums in the second > row. I just want a vector of the sums, and I can't figure out a way > to index the "numeric" object. > > Thanks! > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Gavin Simpson
2008-Feb-29 17:15 UTC
[R] Column sums from a data frame (without the headers)
On Fri, 2008-02-29 at 12:02 -0500, Jason Horn wrote:> Does anyone know how to get a vector of column sum from a data frame? > You can use colSums(), but this gives you a object of type "numeric" > with the column labels in the first row, and the sums in the second > row. I just want a vector of the sums, and I can't figure out a way > to index the "numeric" object. > > Thanks!The labels you are seeing are just the 'names' attribute of the vector of column sums that is being printed. You can ignore them for most uses such as using them in some computations:> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10)) > colSums(df)A B C 5.982975 5.566133 -4.696136> str(colSums(df))Named num [1:3] 5.98 5.57 -4.70 - attr(*, "names")= chr [1:3] "A" "B" "C"> colSums(df) * 4A B C 23.93190 22.26453 -18.78454 If you really need to get rid of the names:> unname(colSums(df))[1] 5.982975 5.566133 -4.696136 HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Ben Fairbank
2008-Feb-29 17:15 UTC
[R] [PS] Column sums from a data frame (without the headers)
as.vector(col.Sums()) Ben -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jason Horn Sent: Friday, February 29, 2008 11:03 AM To: R-help at stat.math.ethz.ch Subject: [PS] [R] Column sums from a data frame (without the headers) Does anyone know how to get a vector of column sum from a data frame? You can use colSums(), but this gives you a object of type "numeric" with the column labels in the first row, and the sums in the second row. I just want a vector of the sums, and I can't figure out a way to index the "numeric" object. Thanks! ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.