tapply(X, INDICES, FUN=<<see below>>, ..., simplify=T)
When FUN is present, tapply calls FUN for each cell that has any data in it. If FUN returns a single atomic value for each cell (e.g. functions mean or var), then tapply returns a multi-way array containing the values. The array has the same number of dimensions as INDICES has components; the number of levels in a dimension is the number of levels in the corresponding component of INDICES. This is a vector if INDICES has only one component.
If FUN does not return a single atomic value, tapply returns an array of mode "list", whose components are the values of the individual calls to FUN. Another way of saying this is that the result is a list that has a dim attribute (this prints as a list, but you can subscript it like an array).
apply is used to apply a function to sections of an array; lapply and sapply apply a function to a list.
tapply(income, list(cut(age, 5), gender), mean) # 5 by 2 matrix of the mean income for each age-gender combination# generate mean republican votes for regions of the U.S. # category that gives the region for each observation region <- state.region[row(votes.repub)] election <- category(votes.year)[col(votes.repub)] mn <- tapply(votes.repub,list(region,election),mean) round(mn,1) # table of mean vote by region and election positions <- tapply(votes.repub,list(region,election)) # positions is a vector of indices for mn (treated as a vector) residuals <- votes.repub - mn[positions]