I am trying to create R Script to summarize measures in a data frame. I would like it to dynamically respond to changes in the structure of the data frame. For example, I have the following block.
library(plyr)
MyData <- baseball[,cbind("id","h")]
AggHits <- aggregate(x=MyData$h, by=list(MyData[,"id"]), FUN=sum)
This block creates a data frame (AggHits) with the total number of hits (h) for each player (id). Yay
Suppose I want to bring in a team. How do I change the argument so that AggHits has a total number of views for each combination of "id" and "team"? I tried the following, and the second line throws an error: the arguments should be the same length
MyData <- baseball[,cbind("id","team","h")]
AggHits <- aggregate(x=MyData$h, by=list(MyData[,cbind("id","team")]), FUN=sum)
More generally, I would like to write a second line so that it automatically aggregates h with all variables except h. I can generate a list of variables for grouping quite easily with setdiff.
SumOver <- setdiff(colnames(MyData),"h")
AggHits <- aggregate(x=MyData$h, by=list(MyData[,cbind(SumOver)]), FUN=sum)
, , csv, ($) (, , , ..). csv Script .
, ddply, , ddply ; .
!
ANSWER ( )
MyData <- baseball[,cbind("id","team","h")]
SumOver <- setdiff(colnames(MyData),"h")
AggHits <- aggregate(x=MyData$h, by=MyData[SumOver], FUN=sum)