[R] DataFrame help

David Huffer David.Huffer at csosa.gov
Thu Jul 16 21:05:29 CEST 2009


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.




More information about the R-help mailing list