Group by aggregate in R

孤者浪人 提交于 2020-01-23 16:59:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!