Skip to content
Prev 257593 / 398506 Next

Stymied by plyr

Hello, This is my first time trying to use plyr, and I'm getting
nowhere. I have teacher ratings data (1:4), on 10 components, by
external observers and internal observers, in schools in areas. I want
to calculate the percentage of each rating given on each component, by
each type of observer, within each school, within each area. The data
look like this:

    unit area ext.obs rating comp
11 77777    11       0      3    1
12 77777    11       0      4    2
13 77777    11       0      3    3
14 77777    11       0      4    4
15 77777    11       0      3    5
16 77777    11       0      3    6
17 77777    11       0      3    7
18 77777    11       0      3    8
19 77777    11       0      3    9
20 77777    11       0      3   10

I thought this would be a perfect application for plyr. I tried this:

calc.pct <- function(x) {
  table(x)/sum(table(x))
}

pcts <- ddply(test.school, .(area, ext.obs, comp), calc.pct, x=rating)
Error in .fun(piece, ...) : unused argument(s) (piece)

Then I tried this:
 pcts <- ddply(test.school, .(area, ext.obs, comp), .(calc.pct(rating)))
Error in .fun(piece, ...) : attempt to apply non-function

I tried all kinds of other variations but with no success. Can someone
give me some pointers?

Thanks.