[R] tapply & hist

Jason Turner jasont at indigoindustrial.co.nz
Fri May 14 21:28:54 CEST 2004


> ...................................
> # Histograms by technology
> par(mfrow=c(2,3))
> tapply(Pot,SGruppo,hist)
> detach(dati)
>
> It all works great but  tapply(Pot,SGruppo,hist) produces 6 histograms
> with
> the titles and the xlab labels in a generic form, something like
> integer[1],
> integer[2], ....... while I'd like to have each graph indicating the

tapply takes atomic data (usually vectors).  You want to pass rows of a
data frame, so the Pot *and* SGruppo will be sent together; "by()" is very
good for this.  It might be possible (even easy?) to use tapply, but I
just use "by" for these things.

Since dati is your data frame, try this (untested!):

by(dati,dati$SGruppo, function(x,...){
  hist(x$Pot,main=as.character(x$SGruppo[1])) } )

Or, use Lattice:

library(lattice)
histogram( ~ Pot | SGruppo, data=dati)

Cheers

Jason




More information about the R-help mailing list