R语言中aggregate 函数详解

2023-05-17 20:05:22 函数 语言 详解

R语言中aggregate 函数

aggregate函数是数据处理中常用到的函数,具有强大的功能。可以按照要求把数据打组聚合,然后对聚合以后的数据进行加和、求平均等各种操作。具体说明可使用命令:help("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)

到此这篇关于R语言中aggregate 函数详解的文章就介绍到这了,更多相关R语言aggregate 函数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章