Compute or Subtract Statistics by Group

DESCRIPTION:

Compute the group sums, means, variances, standard deviations, or other summaries, for a vector or columns of an array. Or, subtract group means from each entry. Most of these functions are generic.

USAGE:

groupMeans( x, group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupSums(  x, group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupVars(  x, group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupStdevs(x, group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupMaxs(x,   group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupMins(x,   group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupProds(x, group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupRanges(x, group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupAnys(x,   group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
groupAlls(x,   group=NULL, na.rm=F, weights=NULL, repeats=F, ...) 
subtractMeans(x, group=NULL, na.rm=F, weights=NULL) 
 

REQUIRED ARGUMENTS:

x
vector or array. Missing values (NA) are allowed.

OPTIONAL ARGUMENTS:

group
vector the same length as numRows(x). Unique values of this vector determine groups (sets of rows). Statistics are calculated for each group separately, and for each column. The default corresponds to a single group.
na.rm
if FALSE, missing values ( NA) in the input result in missing values in corresponding elements of the output. If TRUE then missing values are omitted from calculations.
weights
vector the same length as numRows(x). If present then weighted statistics are calculated.
repeats
return repeated statistics (one for each observation) or non-repeated statistics (one for each strata). Possible values are T, F, 0, 1, or 2. See "VALUE" below.
...
additional arguments may be passed to some functions. In particular, groupVars and groupStdevs accept arguments unbiased and SumSquares; see .

VALUE:

When repeats=F or 0 then the "group" functions return one statistic value per stratum per column of x . Thus if x is an array, the first dimension of the result is equal to the number of groups, and all other dimensions are the same as for x. If x is a vector, the functions return a vector (if more than one group) or scalar (if one group). The names or row names of the result correspond to the sorted unique values of group.

The exception is groupRanges, which returns two values per stratum. In this case the first dimension of the output corresponds to the two values, the second dimension to groups, and other dimensions as for x.

If repeats=T or 1 the "group" functions return an object with the same dimensions as x: each entry is the value of the statistic for the stratum containing the corresponding entry from x. (For groupRanges a dimension of 2 is prepended.)

If repeats=2, return a list with both the repeated and non-repeated objects. The list has names that match the statistic -- "means" and "repeat.means", for example.

subtractMeans returns a vector or array the same size as x, with value equal to x minus "repeat.means".

SEE ALSO:

, , , .

EXAMPLES:

# Center Mileage data about the averages for each type of vehicle 
subtractMeans(fuel.frame$Mileage,group=fuel.frame$Type) 
 
# same, randomly weighted means 
subtractMeans(fuel.frame$Mileage,group=fuel.frame$Type, weights=runif(60)) 
 
# means and standard deviations for Types Small and Sporty 
groupMeans(fuel.frame$Mileage, group=fuel.frame$Type)[c("Small", "Sporty")] 
groupStdevs(fuel.frame$Mileage, group=fuel.frame$Type)[c("Small", "Sporty")]