[R] by & tapply

Peter Malewski y0004379 at tu-bs.de
Wed Aug 30 11:22:44 CEST 2000


Dear Steve,
	it is hard to answer without a example, which can be
reproduced. I'll try:

I think you have a data with a like:


xx <- data.frame(y=rnorm(100),
                 x1=as.factor(round(runif(100,1,4))),
                 x2=as.factor(round(runif(100,1,4)))
                 )

attach(xx)

##and do a: 

by(y,list(x1,x2),plot)

##if you look at values:

by(y,list(x1,x2),function(x)print(x))

##you see that by passes a vector to plot. 

##you can do a 

xn <- codes(x1)
coplot(y~xn|x2)

##or you use a loop:

par(mfrow=c(2,2))
for( i in unique(codes(x1))) plot.default(x2 [codes(x1)== i]
                                          , y[codes(x1)==i ]
                                          ,main=paste("Code:",i)
                                          )

##of course there might be ways to use tapply, lapply etc.

If you want to know why plot.default is nessary to avoid the boxpots, read
the documentation of R "R-intro" part 3.4 "The class of an object" and
related things.

Peter



On Tue, 29 Aug 2000, Steve Arthur wrote:

> Rers,
> 
> I have been using the function 'by' in such a manner:
> 
> by(LogMetric, list(Loci.Number=Loci.Number, Code.Flag=Code.Flag), plot)
> 
> with 
> 
> par(mfrow=c(5,3)) 
> 
> to produce a single R Graphics: Device with 14 different plots on it as
> described above in my 'by' statement.  Thank you for helping me thus far.
> 
> A similar command using 'tapply' can be written as well.
> 
> My problems being:
> 
> 1) Although several LogMetric values may be plotted against several of 
> the same factors (x-value), they are not listed under the same x-value,
> but as separate points (which does not make sense for an x-y plot).  When
> I just use the plot(x,f) I get boxplots which I don't want, I want to see
> the individual y-points above a particular x-value.
> 
> 2) Using 'tapply' or 'by', can non-numeric x-labels be generate for the
> plots, of which the factors are R factors.  I don't understand why the
> numeric values are appearing in the first place.  
> 
> 3) Is there another, simpler, way to subset data and make intelligible
> plots with R?  This industrial strength stuff takes two seconds in SAS (I
> am not implying anything by that statement).  I would think this would be
> a very common statistical task to complete.
> 
> Thanks,
> 
> steve
> 
> PS Addition to hopefully receiving good "by" and "tapply" examples from
> you,
> 
> 1) Can the 'for' statement 'for (var in seq) expr' use factors as
> agruements?  The example in manual says 'for(i in 1:5)'.  I would want to
> do something like 'for(i in Loci.Number)' w/o being able to specify a
> general range in advance I am lost.  Am I lost?  Unfortunately, CRAN
> rejects my two searches for 'by' and 'for'?
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

----

P.Malewski, Maschplatz 8, 38114 Braunschweig, Tel.: 0531 500965, 
At work: (MH-Hannover): 0511 532 3194 / Fax: 0511 532 3190, 
P.Malewski at tu-bs.de, peter.malewski at gmx.de, malewski.peter at mh-hannover.de.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list