r - Group function by two variables on data.table -
my data looks this
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 male 65 2 science male 35 3 law male 98 4 female 55 5 female 20 6 male 80
i have used following code calculate averages
degrees[, mean(wam, na.rm=t),by=faculty][order(-v1)]
so headings are
faculty vi 65 law 50
etc
any advice on how appreciated.
i break sex also
faculty vi vi male female 65 11 law 50 11
you try
dcast.data.table(students, faculty~sex, fun.aggregate=mean, na.rm=true, value.var='wam') # faculty female male #1: 37.5 72.5 #2: law nan 98.0 #3: science nan 35.0
Comments
Post a Comment