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

Lane, Jim jim.lane at rbc.com
Wed Apr 15 19:50:57 CEST 2009


 Phil: thank you very much, that's exactly what I was looking for! Why I
couldn't have figured that out from the doc for myself is another
question:-)

-Jim

-----Original Message-----
From: Phil Spector [mailto:spector at stat.berkeley.edu] 
Sent: 2009, April, 15 1:21 PM
To: Lane, Jim
Subject: Re: [R] How to apply a function to all combinations of the
values of 2 or more variables?

by(mf,list(mf$centre,mf$complex),summary)

                                        - Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Wed, 15 Apr 2009, 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 ??ectronique est confidentiel et prot???? L'exp??iteur 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??ign??s) est interdite.
> Si vous recevez ce courrier ??ectronique par erreur, veuillez m'en
aviser imm??iatement, par retour de courrier ??ectronique ou par un
autre moyen.
>
> 	[[alternative HTML version deleted]]
>
>
_______________________________________________________________________

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.




More information about the R-help mailing list