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

Mike Lawrence Mike.Lawrence at dal.ca
Wed Apr 15 19:16:05 CEST 2009


Check out plyr:

http://had.co.nz/plyr/

On Wed, Apr 15, 2009 at 2:03 PM, Lane, Jim <jim.lane at rbc.com> 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]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~




More information about the R-help mailing list