| 1 |
\name{genTable} |
| 2 |
\alias{Aggregate} |
| 3 |
\alias{genTable} |
| 4 |
\title{Generic Tables and Data Frames of Descriptive Statistics} |
| 5 |
\description{ |
| 6 |
\code{genTable} creates a table of arbitrary summaries conditional on |
| 7 |
given values of independent variables given by a formula. |
| 8 |
|
| 9 |
\code{Aggregate} does the same, but returns a \code{data.frame} instead. |
| 10 |
|
| 11 |
\code{fapply} is a generic function that dispatches on its \code{data} |
| 12 |
argument. It is called internally by \code{Aggregate} and \code{genTable}. |
| 13 |
Methods for this function can be used to adapt \code{Aggregate} and |
| 14 |
\code{genTable} to data sources other than data frames. |
| 15 |
} |
| 16 |
\usage{ |
| 17 |
Aggregate(formula, data=parent.frame(), subset=NULL, |
| 18 |
names=NULL, addFreq=TRUE, drop = TRUE, as.vars=1, |
| 19 |
\dots) |
| 20 |
|
| 21 |
genTable(formula, data=parent.frame(), subset=NULL, |
| 22 |
names=NULL, addFreq=TRUE,\dots) |
| 23 |
|
| 24 |
} |
| 25 |
\arguments{ |
| 26 |
\item{formula}{a formula. The right hand side includes one or more |
| 27 |
grouping variables separated by '+'. These may be factors, numeric, |
| 28 |
or character vectors. The left hand side may be empty, |
| 29 |
a numerical variable, a factor, or an expression. |
| 30 |
See details below.} |
| 31 |
\item{data}{an environment or data frame or an object coercable into a data frame.} |
| 32 |
\item{subset}{an optional vector specifying a subset of observations |
| 33 |
to be used.} |
| 34 |
\item{names}{an optional character vector giving names to the |
| 35 |
result(s) yielded by the expression on the left hand side of \code{formula}. |
| 36 |
This argument may be redundant if the left hand side results in is a named vector. |
| 37 |
(See the example below.)} |
| 38 |
\item{addFreq}{a logical value. If \code{TRUE} and |
| 39 |
\code{data} is a table or a data frame with a variable |
| 40 |
named "Freq", a call to |
| 41 |
\code{table}, \code{\link{Table}}, \code{\link{percent}}, or \code{\link{nvalid}} |
| 42 |
is supplied by an additional argument \code{Freq} |
| 43 |
and a call to \code{table} is translated into |
| 44 |
a call to \code{Table}. |
| 45 |
} |
| 46 |
\item{drop}{a logical value. If \code{TRUE}, empty groups (i.e. when |
| 47 |
there are no observations in the aggregated data frame that contain |
| 48 |
the defining combination of values or factor levels of the |
| 49 |
conditioning variables in \code{by}) are dropped from the result of |
| 50 |
\code{Aggregate}. Otherwise, result are filled with \code{NA}, where appropriate. |
| 51 |
} |
| 52 |
\item{as.vars}{an integer; relevant only if the left hand side of the formula returns |
| 53 |
an array or a matrix - which dimension (rows, columns, or layers etc.) will transformed to |
| 54 |
variables? Defaults to columns in case of matrices and to the highest dimensional extend |
| 55 |
in case of arrays.} |
| 56 |
\item{\dots}{further arguments, passed to methods or ignored.} |
| 57 |
} |
| 58 |
\details{ |
| 59 |
If an expression is given as left hand side of the formula, its |
| 60 |
value is computed for any combination of values of the values on the |
| 61 |
right hand side. If the right hand side is a dot, then all |
| 62 |
variables in \code{data} are added to the right hand side of the |
| 63 |
formula. |
| 64 |
|
| 65 |
If no expression is given as left hand side, |
| 66 |
then the frequency counts for the respective |
| 67 |
value combinations of the right hand variables are computed. |
| 68 |
|
| 69 |
If a single factor is on the left hand side, then the left hand side is |
| 70 |
translated into an appropriate |
| 71 |
call to \code{table()}. Note that also in this case \code{addFreq} takes effect. |
| 72 |
|
| 73 |
If a single numeric variable is on the left hand side, frequency |
| 74 |
counts weighted by this variable are computed. In these cases, |
| 75 |
\code{genTable} is equivalent to \code{\link[stats]{xtabs}} and |
| 76 |
\code{Aggregate} is equivalent to \code{as.data.frame(xtabs(\dots))}. |
| 77 |
} |
| 78 |
\value{ |
| 79 |
\code{Aggregate} |
| 80 |
results in a data frame with conditional summaries and unique value combinations |
| 81 |
of conditioning variables. |
| 82 |
|
| 83 |
\code{genTable} returns a \link{table}, that is, an array with class \code{"table"}. |
| 84 |
} |
| 85 |
\seealso{\link{aggregate.data.frame}, \link{xtabs}} |
| 86 |
\examples{ |
| 87 |
ex.data <- expand.grid(mu=c(0,100),sigma=c(1,10))[rep(1:4,rep(100,4)),] |
| 88 |
ex.data <- within(ex.data, |
| 89 |
x<-rnorm( |
| 90 |
n=nrow(ex.data), |
| 91 |
mean=mu, |
| 92 |
sd=sigma |
| 93 |
) |
| 94 |
) |
| 95 |
|
| 96 |
Aggregate(~mu+sigma,data=ex.data) |
| 97 |
Aggregate(mean(x)~mu+sigma,data=ex.data) |
| 98 |
Aggregate(mean(x)~mu+sigma,data=ex.data,name="Average") |
| 99 |
Aggregate(c(mean(x),sd(x))~mu+sigma,data=ex.data) |
| 100 |
Aggregate(c(Mean=mean(x),StDev=sd(x),N=length(x))~mu+sigma,data=ex.data) |
| 101 |
genTable(c(Mean=mean(x),StDev=sd(x),N=length(x))~mu+sigma,data=ex.data) |
| 102 |
|
| 103 |
Aggregate(table(Admit)~.,data=UCBAdmissions) |
| 104 |
Aggregate(Table(Admit,Freq)~.,data=UCBAdmissions) |
| 105 |
Aggregate(Admit~.,data=UCBAdmissions) |
| 106 |
Aggregate(percent(Admit)~.,data=UCBAdmissions) |
| 107 |
Aggregate(percent(Admit)~Gender,data=UCBAdmissions) |
| 108 |
Aggregate(percent(Admit)~Dept,data=UCBAdmissions) |
| 109 |
Aggregate(percent(Gender)~Dept,data=UCBAdmissions) |
| 110 |
Aggregate(percent(Admit)~Dept,data=UCBAdmissions,Gender=="Female") |
| 111 |
genTable(percent(Admit)~Dept,data=UCBAdmissions,Gender=="Female") |
| 112 |
} |
| 113 |
\keyword{misc} |