问题
I have a CSV file with the following data:
ID Name Batch Marks Grade
1 A 2010 43 C
2 B 2011 88 A
3 C 2011 89 A
4 D 2010 47 C
5 E 2011 82 A
Using R language, I need to take aggregate of the marks of students for the individual batches. What can I use as Rollup function is not available any more in R? I really dont know how to start. Please help.
回答1:
aggregate is a good choice here.
with(nameOfYourDataFrame, aggregate(X=Marks, by=list(Batch), FUN=sum))
aggregate takes three arguments:
X: the continuous variable, and what's passed to the function, FUN
by: one or more discrete variables (aka factors), the values of which determine the 'partitions' that the function, FUN, evaluates
FUN: the function which is passed the continuous variable, X, and calculates one result for each value (or level) for each discrete variable passed in for the parameter, by
not sure from context what rollup function you have in mind; in any event, if it is not sum, then any built-in, eg mean or user-defined fn will work
the function summarize in the package Hmisc is another function to do the same thing.
来源:https://stackoverflow.com/questions/19900415/group-by-aggregate-in-r