Lane, Jim
2009-Jun-26 13:54 UTC
[R] How to create separate plots for all combinations of some factors
Hi, All I have a data frame as follows:> data.class(tapes)[1] "data.frame"> names(tapes)[1] "date" "loc" "class" "drp" "data" "scratch" "reclaim" "total" Date is a date; loc, class and drp are factors; the rest are numerics. I want to generate separate plots by date for the numeric variables for all combinations of the factors. I tried to do this as follows:> by(tapes,list(loc,class,drp),plot(date,scratch))Error in FUN(X[[1L]], ...) : could not find function "FUN" As well as the error I get a single plot for all values of scratch and date which is meaningless. What am I going wrong here? For clarification what I want would be done is SAS as something like: proc gplot; plot scratch*date; by loc class drp; quit; run; 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]]
David Winsemius
2009-Jun-26 15:55 UTC
[R] How to create separate plots for all combinations of some factors
You did not supply any executable examples for testing, but perhaps you could benefit by looking at: ?expand.grid ?tapply Those to split or index your data "for all combinations of factors". ?mapply # to do the plotting. On Jun 26, 2009, at 9:54 AM, Lane, Jim wrote:> Hi, All > > I have a data frame as follows: > >> data.class(tapes) > [1] "data.frame" >> names(tapes) > [1] "date" "loc" "class" "drp" "data" "scratch" > "reclaim" "total" > > Date is a date; loc, class and drp are factors; the rest are numerics. > I want to generate separate plots by date for the numeric variables > for > all combinations of the factors. I tried to do this as follows: > >> by(tapes,list(loc,class,drp),plot(date,scratch)) > Error in FUN(X[[1L]], ...) : could not find function "FUN"Welcome to functional programming. Generally with R when you pass arguments, you need to create a function that receives those arguments, unless you happen to know that the proper objects would be coming to plot (and then you would not use "(date, scratch)". Perhaps (ObWAG, untested, complete guesswork) : by(tapes, list(loc,class,drp), function(x) { with(x, plot(date,scratch))} ) (I generally try tapply() first rather than using by().) # What gets passed to the function is not named "date" and "scratch" in any case, but is named whatever you use in the function argument list. You need to pull that object apart properly within the function.> > As well as the error I get a single plot for all values of scratch and > date which is meaningless. What am I going wrong here? > > For clarification what I want would be done is SAS as something like: >An executable example would have been a better way of getting an accurate answer. -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Reasonably Related Threads
- By= levels with the Hmisc summarize function.
- How to make a time series object from a data frame containing dates.
- How to apply a function to all combinations of the values of 2 or more variables?
- How to concatenate values from a time series and a forecast
- Rsync Error Code 23?