R语言 summary()函数、sapply,Hmisc包中的describe() ,pastecs来获取描述性统计量

> myvars <- c("mpg", "hp", "wt") 
> summary(mtcars[myvars]) 
 mpg hp wt 
 Min. :10.4 Min. : 52.0 Min. :1.51 
 1st Qu.:15.4 1st Qu.: 96.5 1st Qu.:2.58 
 Median :19.2 Median :123.0 Median :3.33 
 Mean :20.1 Mean :146.7 Mean :3.22 
 3rd Qu.:22.8 3rd Qu.:180.0 3rd Qu.:3.61 
 Max. :33.9 Max. :335.0 Max. :5.42 

summary()函数提供了最小值、最大值、四分位数和数值型变量的均值,以及因子向量和逻
辑型向量的频数统计

> mystats <- function(x, na.omit=FALSE){ 
 if (na.omit) 
 x <- x[!is.na(x)] 
 m <- mean(x) 
 n <- length(x) 
 s <- sd(x) 
 skew <- sum((x-m)^3/s^3)/n 
 kurt <- sum((x-m)^4/s^4)/n - 3 
 return(c(n=n, mean=m, stdev=s, skew=skew, kurtosis=kurt)) 
 } 
> myvars <- c("mpg", "hp", "wt") 
> sapply(mtcars[myvars], mystats) 
 mpg hp wt 
n 32.000 32.000 32.0000 
mean 20.091 146.688 3.2172 
stdev 6.027 68.563 0.9785 
skew 0.611 0.726 0.4231 
kurtosis -0.373 -0.136 -0.0227 
上一篇:Transport-CC草稿


下一篇:ABAP语法基础篇(1)——数据类型和对象