Lane, Jim
2009-Apr-15 17:03 UTC
[R] How to apply a function to all combinations of the values of 2 or more variables?
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]]
baptiste auguie
2009-Apr-15 17:15 UTC
[R] How to apply a function to all combinations of the values of 2 or more variables?
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
Mike Lawrence
2009-Apr-15 17:16 UTC
[R] How to apply a function to all combinations of the values of 2 or more variables?
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. ~
Lane, Jim
2009-Apr-15 17:50 UTC
[R] How to apply a function to all combinations of the values of 2 or more variables?
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 doesnot waive any related rights and obligations.> Any distribution, use or copying of this e-mail or the information itcontains by other than an intended recipient is unauthorized.> If you received this e-mail in error, please advise me (by returne-mail or otherwise) immediately.> > Ce courrier ??ectronique est confidentiel et prot???? L'exp??iteur nerenonce pas aux droits et obligations qui s'y rapportent.> Toute diffusion, utilisation ou copie de ce message ou desrenseignements 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'enaviser 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.