R语言分组数据统计2021.5.28
2021-05-28 本文已影响0人
R语言_茶味先生
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




