Groups memisc 0.99.25.4

Operate on grouped data in data frames and data sets

Description

Group creates a grouped variant of an object of class “data.frame” or of class “data.set”, for which methods for with and within are defined, so that these well-known functions can be applied “groupwise”.

Usage

# Create an object of class "grouped.data" from a
# data frame or a data set.
Groups(data,by,...)
## S4 method for signature 'data.frame'
Groups(data,by,...)
## S4 method for signature 'data.set'
Groups(data,by,...)
## S4 method for signature 'grouped.data'
Groups(data,by,...)

# Recombine grouped data into a data fame or a data set
recombine(x,...)
## S4 method for signature 'grouped.data.frame'
recombine(x,...)
## S4 method for signature 'grouped.data.set'
recombine(x,...)

# Recombine grouped data and coerce the result appropriately:
## S4 method for signature 'grouped.data'
as.data.frame(x,...)
## S4 method for signature 'grouped.data.frame'
as.data.set(x,row.names=NULL,...)
## S4 method for signature 'grouped.data.set'
as.data.set(x,row.names=NULL,...)

# Methods of the generics "with" and "within" for grouped data
## S4 method for signature 'grouped.data'
with(data,expr,...)
## S4 method for signature 'grouped.data'
within(data,expr,recombine=FALSE,...)

# This is equivalent to with(Groups(data,by),expr,...)
withGroups(data,by,expr,...)
# This is equivalent to within(Groups(data,by),expr,recombine,...)
withinGroups(data,by,expr,recombine=TRUE,...)

Arguments

data

an object of the classes “data.frame”, “data.set” if an argument to Groups, withGroups, withinGroups,

by

a formula with the factors the levels of which define the groups.

expr

an expression, or several expressions enclosed in curly braces.

recombine

a logical vector; should the resulting grouped data be recombined?

x

an object of class “grouped.data”.

row.names

an optional character vector with row names.

...

other arguments, ignored.

Details

When applied to a data frame Groups returns an object with class attributes “grouped.data.frame”, “grouped.data”, and “data.frame”, when applied do an object with class “data.set”, it returns an object with class attributes “grouped.data.set”, “grouped.data”, and “data.set”.

When applied to objects with class attributed “grouped.data”, both the functions with() amd within() evaluate expr separately for each group defined by Groups. with() returns an array composed of the results of expr, while within() returns a modified copy of its data argument, which will be a “grouped.data” object (“grouped.data.frame” or “grouped.data.set”), unless the argument recombine=TRUE is set.

The expression expr may contain references to the variables n_, N_, and i_. n_ is equal to the size of the respective group (the number of rows belonging to it), while N_ is equal to the total number of observations in all groups. The variable i_ equals to the indices of the rows belonging to the respective group of observations.

Examples

some.data <- data.frame(x=rnorm(n=100))
some.data <- within(some.data,{
   f <- factor(rep(1:4,each=25),labels=letters[1:4])
   g <- factor(rep(1:5,each=4,5),labels=LETTERS[1:5])
   y <- x + rep(1:4,each=25) +  0.75*rep(1:5,each=4,5)
})
# For demonstration purposes, we create an
# 'empty' group:
some.data <- subset(some.data,
                      f!="a" | g!="C")
some.grouped.data <- Groups(some.data,
                          ~f+g)

# Computing the means of y for each combination f and g
group.means <- with(some.grouped.data,
                   mean(y))
group.means
   g
f          A        B        C        D        E
  a 1.949138 2.762734       NA 3.545834 5.335412
  b 2.873753 3.261559 4.705605 4.615220 5.831610
  c 4.005408 4.534578 5.229476 6.457484 6.633849
  d 5.063309 5.537042 5.813667 6.839864 7.047214
# Obtaining a groupwise centered variant of y
some.grouped.data <- within(some.grouped.data,{
   y.cent <- y - mean(y)
},recombine=FALSE)

# The groupwise centered variable should have zero mean
# whithin each group
group.means <- with(some.grouped.data,
                   round(mean(y.cent),15))
group.means
   g
f   A B  C D E
  a 0 0 NA 0 0
  b 0 0  0 0 0
  c 0 0  0 0 0
  d 0 0  0 0 0
# The following demonstrates the use of n_, N_, and i_
# An external copy of y
y1 <- some.data$y
group.means.n <- with(some.grouped.data,
                     c(mean(y),  # Group means for y
                       n_,       # Group sizes
                       sum(y)/n_,# Group means for y
                       n_/N_,    # Relative group sizes
                       sum(y1)/N_,# NOT the grand mean
                       sum(y1[i_])/n_)) # Group mean for y1
group.means.n
, , g = A

                f
                          a          b          c          d
  mean(y)        1.94913848 2.87375288 4.00540792 5.06330854
  n_             8.00000000 4.00000000 4.00000000 4.00000000
  sum(y)/n_      1.94913848 2.87375288 4.00540792 5.06330854
  n_/N_          0.08333333 0.04166667 0.04166667 0.04166667
  sum(y1)/N_     4.82069083 4.82069083 4.82069083 4.82069083
  sum(y1[i_])/n_ 1.94913848 2.87375288 4.00540792 5.06330854

, , g = B

                f
                          a          b          c          d
  mean(y)        2.76273371 3.26155917 4.53457786 5.53704218
  n_             5.00000000 7.00000000 4.00000000 4.00000000
  sum(y)/n_      2.76273371 3.26155917 4.53457786 5.53704218
  n_/N_          0.05208333 0.07291667 0.04166667 0.04166667
  sum(y1)/N_     4.82069083 4.82069083 4.82069083 4.82069083
  sum(y1[i_])/n_ 2.76273371 3.26155917 4.53457786 5.53704218

, , g = C

                f
                  a        b        c          d
  mean(y)        NA 4.705605 5.229476 5.81366702
  n_             NA 6.000000 6.000000 4.00000000
  sum(y)/n_      NA 4.705605 5.229476 5.81366702
  n_/N_          NA 0.062500 0.062500 0.04166667
  sum(y1)/N_     NA 4.820691 4.820691 4.82069083
  sum(y1[i_])/n_ NA 4.705605 5.229476 5.81366702

, , g = D

                f
                          a          b          c          d
  mean(y)        3.54583403 4.61522000 6.45748399 6.83986450
  n_             4.00000000 4.00000000 7.00000000 5.00000000
  sum(y)/n_      3.54583403 4.61522000 6.45748399 6.83986450
  n_/N_          0.04166667 0.04166667 0.07291667 0.05208333
  sum(y1)/N_     4.82069083 4.82069083 4.82069083 4.82069083
  sum(y1[i_])/n_ 3.54583403 4.61522000 6.45748399 6.83986450

, , g = E

                f
                          a          b          c          d
  mean(y)        5.33541156 5.83160979 6.63384942 7.04721404
  n_             4.00000000 4.00000000 4.00000000 8.00000000
  sum(y)/n_      5.33541156 5.83160979 6.63384942 7.04721404
  n_/N_          0.04166667 0.04166667 0.04166667 0.08333333
  sum(y1)/N_     4.82069083 4.82069083 4.82069083 4.82069083
  sum(y1[i_])/n_ 5.33541156 5.83160979 6.63384942 7.04721404
# Names can be attached to the groupwise results
with(some.grouped.data,
    c(Centered=round(mean(y.cent),15),
      Uncentered=mean(y)))
, , g = A

            f
                    a        b        c        d
  Centered   0.000000 0.000000 0.000000 0.000000
  Uncentered 1.949138 2.873753 4.005408 5.063309

, , g = B

            f
                    a        b        c        d
  Centered   0.000000 0.000000 0.000000 0.000000
  Uncentered 2.762734 3.261559 4.534578 5.537042

, , g = C

            f
              a        b        c        d
  Centered   NA 0.000000 0.000000 0.000000
  Uncentered NA 4.705605 5.229476 5.813667

, , g = D

            f
                    a       b        c        d
  Centered   0.000000 0.00000 0.000000 0.000000
  Uncentered 3.545834 4.61522 6.457484 6.839864

, , g = E

            f
                    a       b        c        d
  Centered   0.000000 0.00000 0.000000 0.000000
  Uncentered 5.335412 5.83161 6.633849 7.047214
some.data.ungrouped <- recombine(some.grouped.data)
str(some.data.ungrouped)
'data.frame':        96 obs. of  5 variables:
 $ x     : num  1.1875 -0.0731 -0.9252 0.9556 -1.2307 ...
 $ y     : num  2.937 1.677 0.825 2.706 1.269 ...
 $ g     : Factor w/ 5 levels "A","B","C","D",..: 1 1 1 1 2 2 2 2 4 4 ...
 $ f     : Factor w/ 4 levels "a","b","c","d": 1 1 1 1 1 1 1 1 1 1 ...
 $ y.cent: num  0.988 -0.272 -1.124 0.756 -1.493 ...
# It all works with "data.set" objects
some.dataset <- as.data.set(some.data)
some.grouped.dataset <- Groups(some.dataset,~f+g)
with(some.grouped.dataset,
    c(Mean=mean(y),
      Variance=var(y)))
, , g = A

          f
                   a         b        c          d
  Mean     1.9491385 2.8737529 4.005408 5.06330854
  Variance 0.6657236 0.0757821 0.899300 0.07748734

, , g = B

          f
                  a        b         c         d
  Mean     2.762734 3.261559 4.5345779 5.5370422
  Variance 1.134008 2.419921 0.9154213 0.4063937

, , g = C

          f
            a         b         c         d
  Mean     NA 4.7056046 5.2294757 5.8136670
  Variance NA 0.8958715 0.5666333 0.4498829

, , g = D

          f
                  a        b        c         d
  Mean     3.545834 4.615220 6.457484 6.8398645
  Variance 1.422775 1.257478 1.831166 0.9775665

, , g = E

          f
                  a         b         c         d
  Mean     5.335412 5.8316098 6.6338494 7.0472140
  Variance 1.060255 0.2582544 0.2910714 0.3090965
# The following two expressions are equivalent:
with(Groups(some.data,~f+g),mean(y))
   g
f          A        B        C        D        E
  a 1.949138 2.762734       NA 3.545834 5.335412
  b 2.873753 3.261559 4.705605 4.615220 5.831610
  c 4.005408 4.534578 5.229476 6.457484 6.633849
  d 5.063309 5.537042 5.813667 6.839864 7.047214
withGroups(some.data,~f+g,mean(y))
   g
f          A        B        C        D        E
  a 1.949138 2.762734       NA 3.545834 5.335412
  b 2.873753 3.261559 4.705605 4.615220 5.831610
  c 4.005408 4.534578 5.229476 6.457484 6.633849
  d 5.063309 5.537042 5.813667 6.839864 7.047214
# The following two expressions are equivalent:
some.data <- within(Groups(some.data,~f+g),{
   y.cent <- y - mean(y)
   y.cent.1 <- y - sum(y)/n_
})
some.data <- withinGroups(some.data,~f+g,{
   y.cent <- y - mean(y)
   y.cent.1 <- y - sum(y)/n_
})

# Both variants of groupwise centred varaibles should
# have zero groupwise means:
withGroups(some.data,~f+g,{
   c(round(mean(y.cent),15),
     round(mean(y.cent.1),15))
})
, , g = A

   f
    a b c d
  1 0 0 0 0
  2 0 0 0 0

, , g = B

   f
    a b c d
  1 0 0 0 0
  2 0 0 0 0

, , g = C

   f
     a b c d
  1 NA 0 0 0
  2 NA 0 0 0

, , g = D

   f
    a b c d
  1 0 0 0 0
  2 0 0 0 0

, , g = E

   f
    a b c d
  1 0 0 0 0
  2 0 0 0 0