Re: [R] Applying a function to all combinations of factors

From: <jeaneid_at_chass.utoronto.ca>
Date: Thu 03 Mar 2005 - 06:55:23 EST

you could use something like

by(data, list(data1$day, data1$hour), function(x) cor(x[,"var1"], x[, "var2"]))

This will return a list and then you can unlist and turn to matrix

HTH
> Is there a way to apply a function, say cor(), to each combination of some
> number of variables, and this, without using loops?
>
> For example, I have day, hour, var1 and var2. How could I compute
> cor(var1,var2) for each day*hour combination and obtain a matrix with day,
> hour and the cor value for each combination?
>
> Thanks for your time,
>
> Marc
>
> ==================Marc Bélisle
> Professeur adjoint
> Chaire de recherche du Canada en écologie spatiale et en écologie du
> paysage
> Département de biologie
> Université de Sherbrooke
> 2500 Boul. de l'Université
> Sherbrooke, Québec
> J1K 2R1 Canada
>
> Tél: +1-819-821-8000 poste 1313
> Fax: +1-819-821-8049
> Courriél: Marc.M.Belisle@USherbrooke.ca
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Mar 03 07:02:56 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:39 EST