[R] accessing current factor in tapply

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Thu Apr 17 19:36:46 CEST 2003



Bernie McConnell wrote:
> G'Day,
> 
> I want to access in a function called from tapply the current factor.  
> In my example below, all I want to do is to write the current factor on 
> each histogram.  Needless to say my example does not work. I would be 
> grateful for pointers in the right direction.
> 
> Many thanks
> Bernie McConnell
> Sea Mammal Reserach Unit
> 
> 
> cc <- 1:10
> ff <- rep(c("a","b"),5)
> 
> pp<- function(x,f) {
>   hist(x, main=as.character(f))
>   }
> 
> 
> tapply(aa, ff, pp, f=ff)
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

I think it would be easier and more foolproof to use a for loop:

cc <- 1:10
ff <- rep(c("a", "b"), 5)
par(mfrow = c(1, 2))
for(f in unique(ff))
   hist(cc[ff==f], main = as.character(f))

Regards,
Sundar



More information about the R-help mailing list