I'm learning how to use tapply. Now I'm having a go at the following code in which dati contains almost 600 lines, Pot - numeric - are the capacities of power plants and SGruppo - text - the corresponding six technologies ("CCC", "CIC","TGC", "CSC","CPC", "TE"). ..................................................... dati=sqlQuery(canale,"select Id,SGruppo,Classe, NGruppo,ProdNetta,Pot from SintesiQuery") attach(dati) # Grouping by technology tapply(Pot,SGruppo,sum) ................................... # 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 mentioned technologies. I've been trying issuing tech=c("CCC", "CIC","TGC", "CSC","CPC", "TE") tapply(Pot,SGruppo,hist, main=tech) but R prints in each histogram the six values in the title without cycling among them. How can I obtain what I want? Ciao Vittorio to no avail
As another respondent already mentioned, Lattice is probably the way to go on this one but if you do want to use tapply try this: names(Pot) <- SGruppo dummy <- tapply(Pot,SGruppo,function(x)hist(x,main=names(x)[1],xlab=NULL)) Vittorio <v.demartino2 <at> virgilio.it> writes: : : I'm learning how to use tapply. : Now I'm having a go at the following code in which dati contains almost 600 : lines, Pot - numeric - are the capacities of power plants and SGruppo - text : - the corresponding six technologies ("CCC", "CIC","TGC", "CSC","CPC", "TE"). : ..................................................... : : dati=sqlQuery(canale,"select Id,SGruppo,Classe, NGruppo,ProdNetta,Pot from : SintesiQuery") : attach(dati) : # Grouping by technology : tapply(Pot,SGruppo,sum) : ................................... : # 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 : mentioned technologies. : I've been trying issuing : tech=c("CCC", "CIC","TGC", "CSC","CPC", "TE") : tapply(Pot,SGruppo,hist, main=tech) : : but R prints in each histogram the six values in the title without cycling : among them. : : How can I obtain what I want? : : Ciao : Vittorio
> ................................... > # 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 thetapply 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