Skip to content
Back to formatted view

Raw Message

Message-ID: <2475.128.100.178.79.1109793323.squirrel@128.100.178.79>
Date: 2005-03-02T19:55:23Z
From: Jean Eid
Subject: Applying a function to all combinations of factors
In-Reply-To: <APEJLJBDGGLPABONJPDMGENECMAA.marc.m.belisle@usherbrooke.ca>

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 at USherbrooke.ca
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
>