[R] looking for a smarter way

Grum, Mikkel M.GRUM at CGIAR.ORG
Tue Aug 21 13:58:13 CEST 2001


This works great and I was able to extend it to the other problem too!
Thanks a lot!!

Cheers,
Mikkel

 -----Original Message-----
From: 	Cleland [mailto:clelancm at UMDNJ.EDU] 
Sent:	21 August 2001 13:31
To:	Grum, Mikkel
Cc:	R-help at hypatia.math.ethz.ch
Subject:	Re: [R] looking for a smarter way

"Grum, Mikkel" wrote:
I have two problems where I've come up with some code that will do the
analysis that I want, but it looks pretty clumsy.  In the first case, I
calculate the variance on five different columns for each of 14 clusters and
get them into one matrix.  I get the job done, but I would have thought that
it could be done in one or two lines, not six, and be generalized so that it
didn't matter how many columns I had.  Any suggestions?
xtap1<-tapply(xcmd[,1],xclu$clustering,var)
xtap2<-tapply(xcmd[,2],xclu$clustering,var)
xtap3<-tapply(xcmd[,3],xclu$clustering,var)
xtap4<-tapply(xcmd[,4],xclu$clustering,var)
xtap5<-tapply(xcmd[,5],xclu$clustering,var)
xtap<-cbind(xtap1,xtap2,xtap3,xtap4,xtap5)
Mikkel:
I think you might be looking for something like this:
xtap <- apply(xcmd[,1:5], 2, function(x){tapply(x, list(xclu$clustering),
var)})
Hope this helps,

Chuck

-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-
Chuck Cleland
Institute for the Study of Child Development UMDNJ-Robert Wood Johnson
Medical School 97 Paterson St.
New Brunswick, NJ 08903
phone: (732) 235-7699
fax: (732) 235-6189
http://www2.umdnj.edu/iscdweb/
http://members.nbci.com/cmcleland
-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list