Skip to content
Prev 257598 / 398506 Next

Stymied by plyr

Hi:

The example below uses both the plyr and reshape packages. I'm
presuming that you expect the table proportions to comprise columns of
the output data frame - to do that, we'll use the cast() function from
reshape.

Writing functions to pass into a plyr function needs to be done with a
bit of care. When writing a function for ddply(), the output of the
function must be one of (a) a scalar; (b) a named vector; or (c) a
data frame. A simplified version of your problem is illustrated below;
the ratings variable is coerced to a factor with a fixed set of levels
so that the output of the (relative) frequency table has the same
length in each subgroup. This will matter when it comes to reshaping
the data frame.

library(plyr)
library(reshape2)   # more recent version of the original reshape package
# Example data frame: three schools, two components, 100 observations per school
df <- data.frame(school = rep(LETTERS[1:3], each = 100),
                 component = rep(rep(1:2, each = 50), 3),
                 rating = factor(sample(1:5, 300, replace = TRUE),
levels = 1:5))

# Function to compute the relative frequencies - the input is a
generic sub-data frame,
# the output is a data frame representation of prop.table()
mktab <- function(df) as.data.frame(prop.table(table(df$rating)))

# Apply the function to the input data frame by school/component subgroups
# Notice that the output has one row for each rating in each
school/component subgroup
(dftab <- ddply(df, .(school, component), mktab))

# reshape dftab to display the proportion of each rating in columns instead
cast(dftab, school + component ~ Var1)

# My result:
  school component    1    2    3    4    5
1      A         1 0.24 0.14 0.34 0.14 0.14
2      A         2 0.12 0.20 0.10 0.28 0.30
3      B         1 0.24 0.22 0.22 0.14 0.18
4      B         2 0.10 0.26 0.16 0.32 0.16
5      C         1 0.24 0.18 0.24 0.20 0.14
6      C         2 0.22 0.10 0.16 0.22 0.30

See
http://www.jstatsoft.org/v21/i12
http://www.jstatsoft.org/v40/i01
http://had.co.nz/plyr/
http://had.co.nz/reshape/

Re the last link, the original reshape package has been enhanced and
manifested in the package reshape2. Since you're new to all of this,
you're better off learning how reshape2 works in conjunction with plyr
and several other of Hadley's packages.

HTH,
Dennis

2011/4/20 Stuart Luppescu <slu at ccsr.uchicago.edu>: