Alright, so I am trying to write my own function to calculate column sums in a matrix. I want the result as a single list with the values. So far I have: csum<-function(m) { a = data.frame(m) s = lapply(a,sum) return(s) } What is the easiest way to have it return in a format such as [1] 6 15 24 ? Thanks. -- View this message in context: http://www.nabble.com/DataFrame-help-tp24521881p24521881.html Sent from the R help mailing list archive at Nabble.com.
The easiest way is to just do something like this:> mdat <- matrix(c(4,2,3, 11,12,13), nrow = 2, ncol=3) > mdat[,1] [,2] [,3] [1,] 4 3 12 [2,] 2 11 13> as.vector ( colSums ( mdat ) )[1] 6 14 25>HTH -- David -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of voidobscura Sent: Thursday, July 16, 2009 2:26 PM To: r-help at r-project.org Subject: [R] DataFrame help Alright, so I am trying to write my own function to calculate column sums in a matrix. I want the result as a single list with the values. So far I have: csum<-function(m) { a = data.frame(m) s = lapply(a,sum) return(s) } What is the easiest way to have it return in a format such as [1] 6 15 24 ? Thanks. -- View this message in context: http://www.nabble.com/DataFrame-help-tp24521881p24521881.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Dear voidobscura, Try either: colSums(mdat) # or apply(mdat, 2, sum) See ?colSums and ?apply for more details. HTH, Jorge On Thu, Jul 16, 2009 at 2:25 PM, voidobscura <nshah171@gmail.com> wrote:> > Alright, so I am trying to write my own function to calculate column sums > in > a matrix. I want the result as a single list with the values. > > So far I have: > > csum<-function(m) > { > a = data.frame(m) > s = lapply(a,sum) > return(s) > } > > What is the easiest way to have it return in a format such as [1] 6 15 24 ? > > Thanks. > -- > View this message in context: > http://www.nabble.com/DataFrame-help-tp24521881p24521881.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
As others noted, you can use the built in function colSums, but you said you're writing your own. Given what you've got so far, that makes the issue one of structuring the output. Try csum <- function(m) { a = data.frame(m) s = lapply(a,sum) unlist(s) } lapply() returns a list, so you have to use unlist() on it in order to restructure the list into a vector. However: Converting the matrix to a dataframe is not needed; you can use the apply() function on the matrix, as suggested by Jorge, in which case the result will already be a vector, so you won't need to unlist(). Then it's much simpler: csum <- function(m) apply(m,2,sum) A couple of other notes: Using return() is unnecessary in such a simple function. Be careful with terminology. You said you want the result as a single "list". But a list, in R, is an object with a particular structure, but *not* the structure you appear to be looking for, since your description "[1] 6 15 24" looks like a vector, not a list. -Don At 11:25 AM -0700 7/16/09, voidobscura wrote:>Alright, so I am trying to write my own function to calculate column sums in >a matrix. I want the result as a single list with the values. > >So far I have: > >csum<-function(m) >{ > a = data.frame(m) > s = lapply(a,sum) > return(s) >} > >What is the easiest way to have it return in a format such as [1] 6 15 24 ? > >Thanks. >-- >View this message in context: >http://*www.*nabble.com/DataFrame-help-tp24521881p24521881.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062