目录
  • R语言中aggregate 函数
    • 001、测试数据框
    • 002、 调用函数

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

R语言中aggregate 函数详解

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 函数详解

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。