R Language - group statistics

https://blog.csdn.net/hongweigg/article/details/49420111

https://blog.csdn.net/sinat_26917383/article/details/51086663

https://blog.csdn.net/u011219650/article/details/41380265

 

aggregate function should be used to the data processing function, simply sql somewhat similar language group by, data can be set to play the polymerization as required, and then subsequent addition and polymerization of the data, averaging, and other operations.

 

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

 

A very simple data structure, a group of people's sex, age and height, you can use aggregate functions to seek gender average age and height

 

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

 

Note several points:

  • Type of character or factor columns do not join together computing, will complain
  • List parameter to be configured by, if more than one field, to the corresponding queue by, and group by a plurality of field is the same reason
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
 

Guess you like

Origin www.cnblogs.com/HISAK/p/11769235.html