相关文章推荐
难过的水龙头  ·  java swing ...·  1 年前    · 
温暖的枕头  ·  Python ...·  1 年前    · 
刚分手的咖啡豆  ·  go - Often restarting ...·  1 年前    · 

aggregate函数应该是数据处理中常用到的函数,简单说有点类似sql语言中的group by,可以按照要求把数据打组聚合,然后对聚合以后的数据进行加和、求平均等各种操作。

x=data.frame(name=c("张三","李四","王五","赵六"),sex=c("M","M","F","F"),age=c(20,40,22,30),height=c(166,170,150,155))

构造一个很简单的数据,一组人的性别、年龄和身高,可以用aggregate函数来求不同性别的平均年龄和身高

aggregate(x[,3:4],by=list(sex=x$sex),FUN=mean)

几个注意点:

  • 字符或者factor类型的列不要一起加入计算,会报错
  • by参数要构造成list,如果有多个字段,by就对应队列,和group by多个字段是同样的道理

x=data.frame(name=c("张三","李四","王五","赵六"),
sex=c("M","M","F","F"),age=c(20,40,22,30),height=c(166,170,150,155))
aggregate(x[,3:4],by=list(sex=x$sex),FUN=mean)
# sex age height
# 1   F  26  152.5
# 2   M  30  168.0 这个 函数 的功能比较强大,它首先将数据进行分组(按行),然后对每一组数据进行 函数 统计,最后把结果组合成一个比较nice的表格返回。根据数据对象不同它有三种用法,分别应用于数据框(data.frame)、公式(formula)和时间序列(ts): aggregate (x, by, FUN, …, simplify = TRUE) aggregate (formula, data, FUN, …, subs 1. 函数 功能 Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form. 将数据拆分为子集,为每个子集计算摘要统计信息,然后以方便的形式返回结果。 2. 函数 语法 aggregate (x, by, FUN, ..., simplify = TRUE, drop = TRUE) 3. 函数 参数 3.1 x an R object. 前言这个 函数 的功能比较强大,它首先将数据进行分组(按行),然后对每一组数据进行 函数 统计,最后把结果组合成一个比较nice的表格返回。根据数据对象不同它有三种用法,分别应用于数据框(data.frame)、公式(formula)和时间序列(ts): aggregate (x, by, FUN, ..., simplify = TRUE) aggregate (formula, data, FUN, ...,... R语言 aggregate 函数 前言这个 函数 的功能比较强大,它首先将数据进行分组(按行),然后对每一组数据进行 函数 统计,最后把结果组合成一个比较nice的表格返回。根据数据对象不同它有三种用法,分别应用于数据框(data.frame)、公式(formula)和时间序列(ts): aggregate (x, by, FUN, ..., simplify = TRUE) aggregate (formula, ... r中 aggregate 在Excel中使用 AGGREGATE 更改功能 (Change Functions with AGGREGATE in Excel) A couple of years ago, we looked at the Excel SUBTOTAL function, and saw how you could allow users to select the function ... // iProdSAT代表满意度 Segment代表组别 aggregate (iProdSAT ~ Segment, satData, mean) Segment iProdSAT 1 1 3.462963 2 2 3.725191 3 3 4.103896 4 ... R语言 aggregate 函数 这个 函数 的功能比较强大,它首先将数据进行分组(按行),然后对每一组数据进行 函数 统计,最后把结果组合成一个比较nice的表格返回。根据数据对象不同它有三种用法,分别应用于数据框(data.frame)、公式(formula)和时间序列(ts): aggregate (x, by, FUN, ..., simplify = TRUE) aggre... 以下演示 aggregate 函数 的用法示例 aggregate (x,by, FUN) x是待折叠的数据对象by是变量名组成的list,FUN是 函数 options(digits=3)##小数点位数attach(mtcars)#查看数据结构class(mtcars)## [1] "data.frame"dim(mtcars)## [1] 32 11head(mtcars)## ...