| 1 |
\name{Groups} |
| 2 |
\alias{Groups} |
| 3 |
\alias{Groups.data.set} |
| 4 |
\alias{Groups.data.frame} |
| 5 |
\alias{grouped.data} |
| 6 |
\alias{Groups.grouped.data} |
| 7 |
\alias{with.grouped.data} |
| 8 |
\alias{within.grouped.data} |
| 9 |
\alias{recombine} |
| 10 |
\alias{recombine.grouped.data.frame} |
| 11 |
\alias{recombine.grouped.data.set} |
| 12 |
\alias{as.data.frame.grouped.data} |
| 13 |
\alias{as.data.set,grouped.data.frame-method} |
| 14 |
\alias{as.data.set,grouped.data.set-method} |
| 15 |
\alias{withGroups} |
| 16 |
\alias{withinGroups} |
| 17 |
\title{Operate on grouped data in data frames and data sets} |
| 18 |
\description{\code{Group} creates a grouped variant of an object of |
| 19 |
class "data.frame" or of class "data.set", for which methods for |
| 20 |
\code{with} and \code{within} are defined, so that these well-known |
| 21 |
functions can be applied "groupwise". |
| 22 |
} |
| 23 |
\usage{ |
| 24 |
# Create an object of class "grouped.data" from a |
| 25 |
# data frame or a data set. |
| 26 |
Groups(data,by,\dots) |
| 27 |
\method{Groups}{data.frame}(data,by,\dots) |
| 28 |
\method{Groups}{data.set}(data,by,\dots) |
| 29 |
\method{Groups}{grouped.data}(data,by,\dots) |
| 30 |
|
| 31 |
# Recombine grouped data into a data fame or a data set |
| 32 |
recombine(x,\dots) |
| 33 |
\method{recombine}{grouped.data.frame}(x,\dots) |
| 34 |
\method{recombine}{grouped.data.set}(x,\dots) |
| 35 |
|
| 36 |
# Recombine grouped data and coerce the result appropriately: |
| 37 |
\method{as.data.frame}{grouped.data}(x,\dots) |
| 38 |
\S4method{as.data.set}{grouped.data.frame}(x,row.names=NULL,\dots) |
| 39 |
\S4method{as.data.set}{grouped.data.set}(x,row.names=NULL,\dots) |
| 40 |
|
| 41 |
# Methods of the generics "with" and "within" for grouped data |
| 42 |
\method{with}{grouped.data}(data,expr,\dots) |
| 43 |
\method{within}{grouped.data}(data,expr,recombine=FALSE,\dots) |
| 44 |
|
| 45 |
# This is equivalent to with(Groups(data,by),expr,...) |
| 46 |
withGroups(data,by,expr,\dots) |
| 47 |
# This is equivalent to within(Groups(data,by),expr,recombine,...) |
| 48 |
withinGroups(data,by,expr,recombine=TRUE,\dots) |
| 49 |
} |
| 50 |
|
| 51 |
\arguments{ |
| 52 |
\item{data}{an object of the classes "data.frame", "data.set" if an |
| 53 |
argument to \code{Groups}, \code{withGroups}, \code{withinGroups}, |
| 54 |
} |
| 55 |
\item{by}{a formula with the factors the levels of which define the |
| 56 |
groups.} |
| 57 |
\item{expr}{an expression, or several expressions enclosed in curly |
| 58 |
braces.} |
| 59 |
\item{recombine}{a logical vector; should the resulting grouped |
| 60 |
data be recombined?} |
| 61 |
\item{x}{an object of class "grouped.data".} |
| 62 |
\item{row.names}{an optional character vector with row names.} |
| 63 |
\item{\dots}{other arguments, ignored.} |
| 64 |
} |
| 65 |
|
| 66 |
\details{ |
| 67 |
When applied to a data frame \code{Groups} returns an object with class attributes |
| 68 |
"grouped.data.frame", "grouped.data", and "data.frame", when applied do an object with class |
| 69 |
"data.set", it returns an object with class attributes "grouped.data.set", |
| 70 |
"grouped.data", and "data.set". |
| 71 |
|
| 72 |
When applied to objects with class attributed |
| 73 |
"grouped.data", both the functions \code{with()} amd \code{within()} |
| 74 |
evaluate \code{expr} separately for each group defined by |
| 75 |
\code{Groups}. \code{with()} returns an array composed of the results |
| 76 |
of \code{expr}, while \code{within()} returns a modified copy of its |
| 77 |
\code{data} argument, which will be a "grouped.data" object |
| 78 |
("grouped.data.frame" or "grouped.data.set"), unless the argument |
| 79 |
\code{recombine=TRUE} is set. |
| 80 |
|
| 81 |
The expression \code{expr} may contain references to the variables |
| 82 |
\code{n_}, \code{N_}, and \code{i_}. \code{n_} is equal to the size of |
| 83 |
the respective group (the number of rows belonging to it), while |
| 84 |
\code{N_} is equal to the total number of observations in all |
| 85 |
groups. The variable \code{i_} equals to the indices of the rows |
| 86 |
belonging to the respective group of observations. |
| 87 |
} |
| 88 |
|
| 89 |
\examples{ |
| 90 |
some.data <- data.frame(x=rnorm(n=100)) |
| 91 |
some.data <- within(some.data,{ |
| 92 |
f <- factor(rep(1:4,each=25),labels=letters[1:4]) |
| 93 |
g <- factor(rep(1:5,each=4,5),labels=LETTERS[1:5]) |
| 94 |
y <- x + rep(1:4,each=25) + 0.75*rep(1:5,each=4,5) |
| 95 |
}) |
| 96 |
|
| 97 |
# For demonstration purposes, we create an |
| 98 |
# 'empty' group: |
| 99 |
some.data <- subset(some.data, |
| 100 |
f!="a" | g!="C") |
| 101 |
|
| 102 |
some.grouped.data <- Groups(some.data, |
| 103 |
~f+g) |
| 104 |
|
| 105 |
# Computing the means of y for each combination f and g |
| 106 |
group.means <- with(some.grouped.data, |
| 107 |
mean(y)) |
| 108 |
group.means |
| 109 |
|
| 110 |
# Obtaining a groupwise centered variant of y |
| 111 |
some.grouped.data <- within(some.grouped.data,{ |
| 112 |
y.cent <- y - mean(y) |
| 113 |
},recombine=FALSE) |
| 114 |
|
| 115 |
# The groupwise centered variable should have zero mean |
| 116 |
# whithin each group |
| 117 |
group.means <- with(some.grouped.data, |
| 118 |
round(mean(y.cent),15)) |
| 119 |
group.means |
| 120 |
|
| 121 |
# The following demonstrates the use of n_, N_, and i_ |
| 122 |
# An external copy of y |
| 123 |
y1 <- some.data$y |
| 124 |
group.means.n <- with(some.grouped.data, |
| 125 |
c(mean(y), # Group means for y |
| 126 |
n_, # Group sizes |
| 127 |
sum(y)/n_,# Group means for y |
| 128 |
n_/N_, # Relative group sizes |
| 129 |
sum(y1)/N_,# NOT the grand mean |
| 130 |
sum(y1[i_])/n_)) # Group mean for y1 |
| 131 |
group.means.n |
| 132 |
|
| 133 |
# Names can be attached to the groupwise results |
| 134 |
with(some.grouped.data, |
| 135 |
c(Centered=round(mean(y.cent),15), |
| 136 |
Uncentered=mean(y))) |
| 137 |
|
| 138 |
some.data.ungrouped <- recombine(some.grouped.data) |
| 139 |
str(some.data.ungrouped) |
| 140 |
|
| 141 |
# It all works with "data.set" objects |
| 142 |
some.dataset <- as.data.set(some.data) |
| 143 |
some.grouped.dataset <- Groups(some.dataset,~f+g) |
| 144 |
|
| 145 |
with(some.grouped.dataset, |
| 146 |
c(Mean=mean(y), |
| 147 |
Variance=var(y))) |
| 148 |
|
| 149 |
# The following two expressions are equivalent: |
| 150 |
with(Groups(some.data,~f+g),mean(y)) |
| 151 |
withGroups(some.data,~f+g,mean(y)) |
| 152 |
|
| 153 |
# The following two expressions are equivalent: |
| 154 |
some.data <- within(Groups(some.data,~f+g),{ |
| 155 |
y.cent <- y - mean(y) |
| 156 |
y.cent.1 <- y - sum(y)/n_ |
| 157 |
}) |
| 158 |
|
| 159 |
some.data <- withinGroups(some.data,~f+g,{ |
| 160 |
y.cent <- y - mean(y) |
| 161 |
y.cent.1 <- y - sum(y)/n_ |
| 162 |
}) |
| 163 |
|
| 164 |
# Both variants of groupwise centred varaibles should |
| 165 |
# have zero groupwise means: |
| 166 |
withGroups(some.data,~f+g,{ |
| 167 |
c(round(mean(y.cent),15), |
| 168 |
round(mean(y.cent.1),15)) |
| 169 |
}) |
| 170 |
|
| 171 |
} |