- 2021-02-20 15:06
*views 7*- Data analysis

We often need to group a data according to a certain attribute , Then calculate some statistics . stay R In language ,aggregate Function .

## S3 method for class 'data.frame' aggregate(x, by, FUN, ... simplify =

TRUE, drop = TRUE)

The parameters we often use are ：x, by, FUN.

x, The property or column you want to calculate .

by, It's a list, You can specify one or more columns as the basis for grouping .

FUN, Specify a function , Used to calculate , It can be applied to all grouped data .

If this is our data .

type<-c("a","b","c","a","c","d","b","a","c","b") value<-c(53,15,8,99,76,22,46,

56,34,54) df<-data.frame(type,value) df type value 1 a 53 2 b 15 3 c 8 4 a 99 5

c76 6 d 22 7 b 46 8 a 56 9 c 34 10 b 54

Group sum

aggregate(df$value, by=list(type=df$type),sum) type x 1 a 208 2 b 115 3 c 118

4 d 22

Group average

It's easy to average by groups , Just put the top sum Change to mean That's it .

aggregate(df$value, by=list(type=df$type),mean) type x 1 a 69.33333 2 b

38.33333 3 c 39.33333 4 d 22.00000

Group count , Group counting is statistics in the case of grouping rows Number of .

aggregate(df$value, by=list(type=df$type),length) type x 1 a 3 2 b 3 3 c 3 4 d

1

Group summation based on multiple attributes .

We add a column to the original data , Let's look at multi-attribute grouping .

type_2 <-c("F","M","M","F","F","M","M","F","M","M") df <- data.frame(df, type_2

) df type value type_2 1 a 53 F 2 b 15 M 3 c 8 M 4 a 99 F 5 c 76 F 6 d 22 M 7 b

46 M 8 a 56 F 9 c 34 M 10 b 54 M aggregate(x=df$value, by=list(df$type,df$type_2

),sum) Group.1 Group.2 x 1 a F 208 2 c F 76 3 b M 115 4 c M 42 5 d M 22

Technology

- Python104 blogs
- Java98 blogs
- Flow Chart79 blogs
- Vue51 blogs
- MySQL32 blogs
- programing language32 blogs
- javascript30 blogs
- Linux27 blogs
- more...

Daily Recommendation

©2020 ioDraw All rights reserved

Swing actual combat Understanding closure about vue in el-date-picker type=daterange The problem of date not echoing Ubuntu 18.04 swap Partition expansion Snake code --c Language Edition visual c++6.0 open 【Golang Basic series 10 】Go language On conditional sentences ifString class —— summary ,String The nature of , Memory resolution , Commonly used APIAdobe Illustrator Publish native support Apple Silicon Test version of the software study java My first class antd table sort