[R] accessing current factor in tapply

Thomas W Blackwell tblackw at umich.edu
Thu Apr 17 19:54:38 CEST 2003

I've just been puzzling through this for my own use.
There are three different R functions which do related
things:  tapply(), by() and aggregate().  In this context,
I think I would use  by()  rather than  tapply().  Define
cc, ff as before, and re-define

pp <- function(dat, name)
	hist(dat$x, main=as.character(unique(dat[[name]])))

by(data.frame(cc,ff), ff, pp, "ff")	#  should do what you want

The slightly odd syntax at the end of pp() allows its second
argument "name" to be either a string naming the selection
variable (as here) or else an integer specifying it by position
within the data frame.  If you want to split the data set on
the cross product of two factors, ff x gg, the second argument
to by() would have to be  list(ff,gg).  tapply(), by contrast,
requires the list() syntax even when there's only one factor
to split on.

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Thu, 17 Apr 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)

More information about the R-help mailing list