aggregate 和 order函数
来源:互联网 发布:javascript经典例子 编辑:程序博客网 时间:2024/05/17 02:16
学习R 语言往往学了后面,忘了前面的。这不,画图这章节里面又用到了以前学过的函数。啰嗦着再写一下,权当复习。
1.aggregate 函数
基本语法,aggregate(x,by,FUN,...),主要用到的是前几个类型的。例:
aggregate(mtcars,by=list(mtcars$cyl,mtcars$gear),FUN=mean),此处的by参数要写成list型的。
如果是多个分组的,list里面要隔开写。
> aggregate(mtcars,by=list(mtcars$cyl,mtcars$gear),FUN=mean)
Group.1 Group.2 mpg cyl disp hp drat wt qsec vs am gear carb
1 4 3 21.500 4 120.1000 97.0000 3.700000 2.465000 20.0100 1.0 0.00 3 1.000000
2 6 3 19.750 6 241.5000 107.5000 2.920000 3.337500 19.8300 1.0 0.00 3 1.000000
3 8 3 15.050 8 357.6167 194.1667 3.120833 4.104083 17.1425 0.0 0.00 3 3.083333
4 4 4 26.925 4 102.6250 76.0000 4.110000 2.378125 19.6125 1.0 0.75 4 1.500000
5 6 4 19.750 6 163.8000 116.5000 3.910000 3.093750 17.6700 0.5 0.50 4 4.000000
6 4 5 28.200 4 107.7000 102.0000 4.100000 1.826500 16.8000 0.5 1.00 5 2.000000
7 6 5 19.700 6 145.0000 175.0000 3.620000 2.770000 15.5000 0.0 1.00 5 6.000000
8 8 5 15.400 8 326.0000 299.5000 3.880000 3.370000 14.5500 0.0 1.00 5 6.000000
小数点太多太乱,别急。
>options(digits=2)
> aggregate(mtcars,by=list(mtcars$cyl,mtcars$gear),FUN=mean)
Group.1 Group.2 mpg cyl disp hp drat wt qsec vs am gear carb
1 4 3 22 4 120 97 3.7 2.5 20 1.0 0.00 3 1.0
2 6 3 20 6 242 108 2.9 3.3 20 1.0 0.00 3 1.0
3 8 3 15 8 358 194 3.1 4.1 17 0.0 0.00 3 3.1
4 4 4 27 4 103 76 4.1 2.4 20 1.0 0.75 4 1.5
5 6 4 20 6 164 116 3.9 3.1 18 0.5 0.50 4 4.0
6 4 5 28 4 108 102 4.1 1.8 17 0.5 1.00 5 2.0
7 6 5 20 6 145 175 3.6 2.8 16 0.0 1.00 5 6.0
8 8 5 15 8 326 300 3.9 3.4 15 0.0 1.00 5 6.0
如果aggregate(mtcars,by=list(x=mtcars$cyl,y=mtcars$gear),FUN), 则group.1 ,group.2 则对应成为x,y。
> mm<-aggregate(mtcars,by=list(x=mtcars$cyl,y=mtcars$gear),FUN=mean)
> mm
x y mpg cyl disp hp drat wt qsec vs am gear carb
1 4 3 22 4 120 97 3.7 2.5 20 1.0 0.00 3 1.0
2 6 3 20 6 242 108 2.9 3.3 20 1.0 0.00 3 1.0
3 8 3 15 8 358 194 3.1 4.1 17 0.0 0.00 3 3.1
4 4 4 27 4 103 76 4.1 2.4 20 1.0 0.75 4 1.5
5 6 4 20 6 164 116 3.9 3.1 18 0.5 0.50 4 4.0
6 4 5 28 4 108 102 4.1 1.8 17 0.5 1.00 5 2.0
7 6 5 20 6 145 175 3.6 2.8 16 0.0 1.00 5 6.0
8 8 5 15 8 326 300 3.9 3.4 15 0.0 1.00 5 6.0
注:本例有一点没有体现出来,字符或者factor类型的列不要一起加入计算。如果数据中包含,请剔除对应的列。
2.order 函数
如果我希望mm表中的mpg 按照从小到大的顺序排列改怎么做呢?
mm<-order(mm$mpg)
对吗?
> cc<-order(mm$mpg)
> cc
[1] 3 8 7 2 5 1 4 6
x y mpg cyl disp hp drat wt qsec vs am gear carb
3 8 3 15 8 358 194 3.1 4.1 17 0.0 0.00 3 3.1
8 8 5 15 8 326 300 3.9 3.4 15 0.0 1.00 5 6.0
7 6 5 20 6 145 175 3.6 2.8 16 0.0 1.00 5 6.0
2 6 3 20 6 242 108 2.9 3.3 20 1.0 0.00 3 1.0
5 6 4 20 6 164 116 3.9 3.1 18 0.5 0.50 4 4.0
1 4 3 22 4 120 97 3.7 2.5 20 1.0 0.00 3 1.0
4 4 4 27 4 103 76 4.1 2.4 20 1.0 0.75 4 1.5
6 4 5 28 4 108 102 4.1 1.8 17 0.5 1.00 5 2.0
只是列出了mpg这列中每个元素的位置! 需要再套上一层!
mm<-mm[order(mm$mpg),]
> mm
如果是多个列呢?
> cc<-mm[order(mm$cyl,mm$hp),]
> cc
x y mpg cyl disp hp drat wt qsec vs am gear carb
4 4 4 27 4 103 76 4.1 2.4 20 1.0 0.75 4 1.5
1 4 3 22 4 120 97 3.7 2.5 20 1.0 0.00 3 1.0
6 4 5 28 4 108 102 4.1 1.8 17 0.5 1.00 5 2.0
2 6 3 20 6 242 108 2.9 3.3 20 1.0 0.00 3 1.0
5 6 4 20 6 164 116 3.9 3.1 18 0.5 0.50 4 4.0
7 6 5 20 6 145 175 3.6 2.8 16 0.0 1.00 5 6.0
3 8 3 15 8 358 194 3.1 4.1 17 0.0 0.00 3 3.1
8 8 5 15 8 326 300 3.9 3.4 15 0.0 1.00 5 6.0
可以看出,order 可以跟多个。如果是从大到小,则order里面加上decreasing=TRUE.
- aggregate 和 order函数
- django的aggregate()和annotate()函数用法
- mongodb的聚合函数aggregate的$match和$group方法。
- aggregate 函数使用示例
- Spark函数讲解:aggregate
- spark函数讲解:aggregate
- PySpark-aggregate函数说明
- R aggregate函数
- 聚合函数 Aggregate Function
- MDX Aggregate函数
- spark aggregate函数详解
- reduce,aggregate和groupBy
- MongoDB的聚合函数 Aggregate
- MongoDB的聚合函数 Aggregate
- Cognos函数(二十) - aggregate
- mongodb-aggregate(聚合函数)
- pyspark aggregate函数使用问题(aggregate重写,aggregate中的函数参数限制)
- mongo 的 find和aggregate
- 如何使用蓝牙模块与电脑通信,实现自主开发
- 算法:递归
- MATLAB conv2卷积 一看就懂
- 派尔玻璃优化软件 v3.2.2 免费班
- HDU 1266 Reverse Number
- aggregate 和 order函数
- 动态树 Link Cut Tree
- 华为OJ 初级:判断两个IP是否属于同一子网
- HDU 2028 Lowest Common Multiple Plus
- Linux nice命令详解
- 一次完整的HTTP事务是怎样一个过程?
- 剑指Offer----面试题31:连续子数组的最大和
- 我的第一篇博客
- hdu5772String problem