1. 分组统计
aggregate函数和summaryBy函数都只能描述单一的统计量
1.1 aggregate函数
Cars93是数据框格式。
分组统计时用aggregate函数进行统计描述
library(MASS)
Cars93
aggregate(Cars93[c("Min.Price","Price","Max.Price","MPG.city")],by=list(Manufacturer=Cars93$Manufacturer),mean)
#对生产商进行分组后求Min.Price,Price,Max.Price,MPG.city的均值,见图1
#aggregate(Cars93[c("Min.Price","Price","Max.Price","MPG.city")],by=list(Cars93$Origin),mean)
#对车辆来源进行分组后求Min.Price,Price,Max.Price,MPG.city的均值,见图2
#aggregate(Cars93[c("Min.Price","Price","Max.Price","MPG.city")],by=list(Manufacturer=Cars93$Manufacturer),sd)
#对生产商进行分组后求Min.Price,Price,Max.Price,MPG.city的标准差,见图3
#aggregate(Cars93[c("Min.Price","Price","Max.Price","MPG.city")],by=list(Cars93$Origin),sd)
#对车辆来源进行分组后求Min.Price,Price,Max.Price,MPG.city的标准差,见图4
#aggregate(Cars93[c("Min.Price","Price","Max.Price","MPG.city")],by=list(Origin=Cars93$Origin,Manufacturer=Cars93$Manufacturer),mean)
#对车辆来源和生产商进行分组后求Min.Price,Price,Max.Price,MPG.city的均值,见图5
图1 aggregate函数生产商分组统计均值
图2 aggregate函数车辆来源分组统计均值
图3 aggregate函数生产商分组统计标准差
图4 aggregate函数车辆来源分组统计标准差
图5 aggregate函数车辆来源和生产商分组统计均值
网友评论