|
参考 tapply() :
n <- 17; fac <- factor (rep (1:3 , len = n) , levels = 1:5)
table(fac)
tapply (1:n, fac , sum)
tapply (1:n, fac , mean)
## or r e v e r s e a l i s t
to <- list (a = 1, b = 1, c = 2, d = 1)
tapply(to , unlist (to) , names)
tapply() 的常见于方差分析中对各个组别进行 mean、var(sd)的计算。说到概要统计,不得不说另
外一个函数 aggregate(),它将 tapply() 函数对象为向量的限制扩展到了数据框。
attach(warpbreaks)
tapply(breaks , list (wool , tension ) ,mean)
aggregate(breaks , list (wool , tension ) ,mean)
## from the h e l p
aggregate( state .x77 ,
list (Region = state . region ,
Cold = state .x77[ ,"Frost"] > 130),
mean) |
|