tags:

views:

105

answers:

2

I'm using the "by" function in R to chop up a data frame and apply a function to different parts, like this:

pairwise.compare <- function(x) {
Nright <- ...
Nwrong <- ...
Ntied <- ...
return(c(Nright=Nright, Nwrong=Nwrong, Ntied=Ntied))
}
Z.by <- by(rankings, INDICES=list(rankings$Rater, rankings$Class), FUN=pairwise.compare)

The result (Z.by) looks something like this:

: 4 
: 357 
Nright Nwrong Ntied
     3      0     0
------------------------------------------------------------
: 8 
: 357 
NULL
------------------------------------------------------------
: 10 
: 470 
Nright Nwrong Ntied
     3      4     1 
------------------------------------------------------------ 
: 11 
: 470 
Nright Nwrong Ntied
    12      4     1

What I would like is to have this result converted into a data frame (with the NULL entries not present) so it looks like this:

  Rater Class Nright Nwrong Ntied
1     4   357      3      0     0
2    10   470      3      4     1
3    11   470     12      4     1

How do I do that?

+2  A: 

The by function returns a list, so you can do something like this:

data.frame(do.call("rbind", by(x, column, mean)))
Shane
That almost does what I want, I get a data frame with the Nright, Nwrong and Ntied columns, but it doesn't produce the Rater and Class columns.
lorin
I would suggest changing your `pairwise.compare` function to return those two fields. Otherwise you will have to use an `lapply` routine (or `plyr`) to get both the list names and values (which is an extra step).
Shane
It appears that plyr is actually a simpler solution than by in this case, I just didn't know about that package before.
lorin
+2  A: 

Consider using ddply in the plyr package instead of by. It handles the work of adding the column to your dataframe.

frankc
This is what I ultimately did.
lorin