Here's the code to do it using aggregate():
> junk = matrix(1:(5000*100), ncol=100)
> f = rep(paste("f", 1:100, sep=""), rep(50,100))
> f = ordered(f, levels=paste("f", 1:100, sep=""))
> dos.time(tmp <<- aggregate(junk, by=f, FUN=mean))
[1] 12.918
Here's the code to do it using the generic roll() function in the new
S+FinMetrics module:
> tmp.fun = function(data) {
> list(colMeans(data))
> }
> dos.time(tmp2 <<- roll(tmp.fun, junk, 50, incr=50, trace=F)[[1]])
[1] 0.922
The second approach utilizes the colMeans() function, and thus is much
faster.
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
Jeffrey Wang Research Scientist
Insightful Corp. (206) 802-2269
-----Original Message-----
From: Shin, David [mailto:David_Shin@ctb.com]
Sent: Thursday, June 20, 2002 10:03 AM
To: S-news@wubios.wustl.edu
Subject: [S] about aggregate
Hi,
I am a new user of this list. If I say something wrong, please correct and
pardon me. Thanks.
I have a question about "aggregate".
I have a data matrix that is 5000 (rows) x 100(columns).
I want s-plus to compute the means for every 50 rows so that I can get a new
matrix that is 100 x100.
I try this command: aggregate( my.data, ndeltat = 50, fun = mean) , but it
doesn't work.
If someone knows how to do this, would you please tell me.
Thank you very much.
David
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|