R语言中aggregate 函数

 

001、测试数据框

studentID <- seq(1, 20)
gender <- rep(c("M", "M", "F", "F", "F"), 4)
math <- rep(c(92, 86, 85, 74, 82), 4)
english <- rep(c(76, 69, 82, 71, 80), 4)
class <- rep(c(paste0(c(1, 2, 2, 1),"")), 5)
score <- data.frame(studentID, class, gender, math, english)
dim(score)
head(score

 

002、 调用函数

aggregate(score[,5], by=list(score$gender), mean)
aggregate(score[,5], by=list(score$gender, score$class), mean)
aggregate(score[,5], by=list(score$gender, score$class), sum)
aggregate(score[,5], by=list(score$gender, score$class), max)

 

来源:https://blog.csdn.net/chongbaikaishi/article/details/116131517

 

posted @ 2023-04-26 09:49  小鲨鱼2018  阅读(115)  评论(0编辑  收藏  举报