[R] How to apply a function to all combinations of the values of 2 or more variables?

baptiste auguie ba208 at exeter.ac.uk
Wed Apr 15 19:15:02 CEST 2009


I think you want to have a look at the plyr or doBy packages.

It would be easier to give a precise answer with a minimal example.

HTH,

baptiste

On 15 Apr 2009, at 18:03, Lane, Jim wrote:

> Hi, All
>
> Forgive me if this is a stupid newbie question. I'm having no luck
> googling an answer to this, probably because I don't know the right R
> terminology to frame my question. I want to know how to run an R
> function on each combination of the values of 2 or more variables. In
> SAS-speak this is multiple "BY variables" or "CLASS variables".
>
> In R I've figured out how to do what I want with one by value.
>
> by(mf,mf$centre,summary)
>
> Centre is one of the columns in the data frame "mf" which looks like
> this:
>
> names(mf)
> [1] "centre"   "complex"  "appl"     "pool"     "month"    "alloc_gb"
>
> I'd like to analyze, for example, by complex within centre. This has a
> manageable number of combinations:
>
> table(mf$centre,mf$complex)
>
>                   A      B      C      D      E      F      G      H
> I      J      K      L
>  B         0      0      0      0      0      0      0      0      0
> 0      0  60574      0
>  G        44    209      0  94613      0    156      0   2541      0
> 748      0      0 215511
>  O         0      0      0      0      0      0   3446      0    676
> 0  84400      0      0
>  Q         0      0    298      0  66277      0      0      0      0
> 0      0      0      0
>
> In SAS what I'm after is something like:
>
> Proc summary nway;
> class centre complex;
> var alloc_gb;
> output out=s sum=;
> run;
>
> How do I get something similar in R?
>
> Jim Lane
> Capacity Planner
> RBC Financial Group
> 315 Front St W
> 6th Floor - H14
> Toronto, Ontario CANADA
> M5V 3A4
> 416-348-6024
>
> Display of superior knowledge is as great a vulgarity
> as display of superior wealth - greater indeed, inasmuch
> as knowledge should tend more definitely than wealth
> towards discretion and good manners.
>
> - H. W. Fowler, Modern English Usage
>
>
> _______________________________________________________________________
>
> This e-mail may be privileged and/or confidential, and the sender  
> does not waive any related rights and obligations.
> Any distribution, use or copying of this e-mail or the information  
> it contains by other than an intended recipient is unauthorized.
> If you received this e-mail in error, please advise me (by return e- 
> mail or otherwise) immediately.
>
> Ce courrier électronique est confidentiel et protégé. L'expéditeur  
> ne renonce pas aux droits et obligations qui s'y rapportent.
> Toute diffusion, utilisation ou copie de ce message ou des  
> renseignements qu'il contient par une personne autre que le (les)  
> destinataire(s) désigné(s) est interdite.
> Si vous recevez ce courrier électronique par erreur, veuillez m'en  
> aviser immédiatement, par retour de courrier électronique ou par un  
> autre moyen.
>
> 	[[alternative HTML version deleted]]
>
> <ATT00001.txt>

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list