An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090415/0ed3da4c/attachment-0001.pl>
How to apply a function to all combinations of the values of 2 or more variables?
3 messages · Lane, Jim, Baptiste Auguie, Michael Lawrence
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
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. ~