| 1 |
\name{Means} |
| 2 |
\alias{Means} |
| 3 |
\alias{Means.data.frame} |
| 4 |
\alias{Means.numeric} |
| 5 |
\alias{Means.formula} |
| 6 |
\alias{as.data.frame.means.table} |
| 7 |
\alias{as.data.frame.xmeans.table} |
| 8 |
\title{Means for groups of observations} |
| 9 |
\description{The function \code{Means()} creates a table of group |
| 10 |
means, optionally with standard errors, confidence intervals, and |
| 11 |
numbers of valid observations.} |
| 12 |
\usage{ |
| 13 |
Means(data, \dots) |
| 14 |
\method{Means}{data.frame}(data, |
| 15 |
by, weights=NULL, subset=NULL, |
| 16 |
default=NA, |
| 17 |
se=FALSE, ci=FALSE, ci.level=.95, |
| 18 |
counts=FALSE, \dots) |
| 19 |
\method{Means}{formula}(data, subset, weights, \dots) |
| 20 |
\method{Means}{numeric}(data, \dots) |
| 21 |
\method{as.data.frame}{means.table}(x, row.names=NULL, optional=TRUE, drop=TRUE, \dots) |
| 22 |
\method{as.data.frame}{xmeans.table}(x, row.names=NULL, optional=TRUE, drop=TRUE, \dots) |
| 23 |
} |
| 24 |
\arguments{ |
| 25 |
\item{data}{an object usually containing data, or a formula. |
| 26 |
|
| 27 |
If \code{data} is a numeric vector or an object that can be coerced |
| 28 |
into a data frame, it is changed into a data frame and the data |
| 29 |
frame method of \code{Means()} is applied to it. |
| 30 |
|
| 31 |
If \code{data} is a formula, then a data frame is constructed from |
| 32 |
the variables in the formula and \code{Means} is applied to this |
| 33 |
data frame, while the formula is passed on as a \code{by=} argument. |
| 34 |
} |
| 35 |
\item{by}{a formula, a vector of variable names or a data frame or |
| 36 |
list of factors. |
| 37 |
|
| 38 |
If \code{by} is a vector of variable names, |
| 39 |
they are extracted from \code{data} to define the groups for which |
| 40 |
means are computed, while the variables for which the means are |
| 41 |
computed are those not named in \code{by}. |
| 42 |
|
| 43 |
If \code{by} is a data frame or a list of factors, |
| 44 |
these are used to defined the groups for which means are computed, |
| 45 |
while the variables for which the means are |
| 46 |
computed are those not in \code{by}. |
| 47 |
|
| 48 |
If \code{by} is a formula, its left-hand side determines the |
| 49 |
variables of which means are computed, while its right-hand side |
| 50 |
determines the factors that define the groups. |
| 51 |
} |
| 52 |
\item{weights}{an optional vector of weights, usually a variable in \code{data}.} |
| 53 |
\item{subset}{an optional logical vector to select observations, |
| 54 |
usually the result of an expression in variables from \code{data}.} |
| 55 |
\item{default}{a default value used for empty cells without |
| 56 |
observations.} |
| 57 |
\item{se}{a logical value, indicates whether standard errors should be |
| 58 |
computed.} |
| 59 |
\item{ci}{a logical value, indicates whether limits of confidence |
| 60 |
intervals should be computed.} |
| 61 |
\item{ci.level}{a number, the confidence level of the confidence interval} |
| 62 |
\item{counts}{a logical value, indicates whether numbers of valid |
| 63 |
observations should be reported.} |
| 64 |
\item{x}{for \code{as.data.frame()}, a result of \code{Means()}.} |
| 65 |
\item{row.names}{an optional character vector. This argmument presently is |
| 66 |
inconsequential and only included for reasons of compatiblity |
| 67 |
with the standard methods of \code{\link[base]{as.data.frame}}.} |
| 68 |
\item{optional}{an optional logical value. This argmument presently is |
| 69 |
inconsequential and only included for reasons of compatiblity |
| 70 |
with the standard methods of \code{\link[base]{as.data.frame}}.} |
| 71 |
\item{drop}{a logical value, determines whether "empty cells" should |
| 72 |
be dropped from the resulting data frame.} |
| 73 |
\item{\dots}{other arguments, either ignored or passed on to other |
| 74 |
methods where applicable.} |
| 75 |
} |
| 76 |
\value{ |
| 77 |
An array that inherits classes "means.table" and "table". If |
| 78 |
\code{Means} was called with \code{se=TRUE} or \code{ci=TRUE} |
| 79 |
then the result additionally inherits class "xmeans.table". |
| 80 |
} |
| 81 |
\examples{ |
| 82 |
# Preparing example data |
| 83 |
USstates <- as.data.frame(state.x77) |
| 84 |
USstates <- within(USstates,{ |
| 85 |
region <- state.region |
| 86 |
name <- state.name |
| 87 |
abb <- state.abb |
| 88 |
division <- state.division |
| 89 |
}) |
| 90 |
USstates$w <- sample(runif(n=6),size=nrow(USstates),replace=TRUE) |
| 91 |
|
| 92 |
# Using the data frame method |
| 93 |
Means(USstates[c("Murder","division","region")],by=c("division","region")) |
| 94 |
Means(USstates[c("Murder","division","region")],by=USstates[c("division","region")]) |
| 95 |
Means(USstates[c("Murder")],1) |
| 96 |
Means(USstates[c("Murder","region")],by=c("region")) |
| 97 |
|
| 98 |
# Using the formula method |
| 99 |
# One 'dependent' variable |
| 100 |
Means(Murder~1, data=USstates) |
| 101 |
Means(Murder~division, data=USstates) |
| 102 |
Means(Murder~division, data=USstates,weights=w) |
| 103 |
Means(Murder~division+region, data=USstates) |
| 104 |
as.data.frame(Means(Murder~division+region, data=USstates)) |
| 105 |
|
| 106 |
# Standard errors and counts |
| 107 |
Means(Murder~division, data=USstates, se=TRUE, counts=TRUE) |
| 108 |
drop(Means(Murder~division, data=USstates, se=TRUE, counts=TRUE)) |
| 109 |
as.data.frame(Means(Murder~division, data=USstates, se=TRUE, counts=TRUE)) |
| 110 |
|
| 111 |
# Confidence intervals |
| 112 |
Means(Murder~division, data=USstates, ci=TRUE) |
| 113 |
drop(Means(Murder~division, data=USstates, ci=TRUE)) |
| 114 |
as.data.frame(Means(Murder~division, data=USstates, ci=TRUE)) |
| 115 |
|
| 116 |
# More than one dependent variable |
| 117 |
Means(Murder+Illiteracy~division, data=USstates) |
| 118 |
as.data.frame(Means(Murder+Illiteracy~division, data=USstates)) |
| 119 |
|
| 120 |
# Confidence intervals |
| 121 |
Means(Murder+Illiteracy~division, data=USstates, ci=TRUE) |
| 122 |
as.data.frame(Means(Murder+Illiteracy~division, data=USstates, ci=TRUE)) |
| 123 |
|
| 124 |
# Some 'non-standard' but still valid usages: |
| 125 |
with(USstates, |
| 126 |
Means(Murder~division+region,subset=region!="Northeast")) |
| 127 |
|
| 128 |
with(USstates, |
| 129 |
Means(Murder,by=list(division,region))) |
| 130 |
} |