通过data.table上的两个变量对函数进行分组

时间:2014-12-02 07:06:57

标签: r data.table

我的数据看起来像这样

students<-data.table(studid=c(1:6) ,FACULTY= c("IT","SCIENCE", "LAW","IT","IT","IT"), 
                 SEX=c("Male","Male","Male","Female","Female","Male"), WAM=c(65,35,98,55,20,80))

studid   FACULTY    SEX      AVE_MARK (WAM)
1        IT         Male     65
2        SCIENCE    Male     35
3        LAW        Male     98
4        IT         Female   55
5        IT         Female   20
6        IT         Male     80

我使用以下代码计算平均值

degrees[, mean(WAM, na.rm=T),by=FACULTY][order(-V1)]

所以我的标题是

FACULTY    VI     
IT         65     
LAW        50    

非常感谢任何关于如何做到这一点的建议。

我想通过性别来解决这个问题

FACULTY    VI     VI
           Male   Female
IT         65     11
LAW        50     11

2 个答案:

答案 0 :(得分:1)

你可以尝试

dcast.data.table(students, FACULTY~SEX, fun.aggregate=mean, na.rm=TRUE,
                   value.var='WAM')
#    FACULTY Female Male
#1:      IT   37.5 72.5
#2:     LAW    NaN 98.0
#3: SCIENCE    NaN 35.0

答案 1 :(得分:1)

你肯定需要以交叉表格式吗?如果是这样,那么akrun的答案是可行的。

否则,它们堆叠在一起:

> students[, mean(WAM, na.rm=T),by=c('FACULTY','SEX')]
   FACULTY    SEX   V1
1:      IT   Male 72.5
2: SCIENCE   Male 35.0
3:     LAW   Male 98.0
4:      IT Female 37.5