R语言中有很多有用的统计函数。例如算术平均数:
mean(x)
求x的平均数。
mean(x, trim=0.05, na.rm=TRUE)
则提供了截尾平均数,即丢弃了最大5%和最小5%的数据和所有缺失值后的算术平均数。
R提供的常用统计函数:
| 函 数 | 描述 |
| mean(x) | 平均数 |
| mean(c(1,2,3,4))返回值为2.5 | |
| median(x) | 中位数 |
| median(c(1,2,3,4))返回值为2.5 | |
| sd(x) | 标准差 |
| sd(c(1,2,3,4))返回值为1.29 | |
| var(x) | 方差 |
| var(c(1,2,3,4))返回值为1.67 | |
| mad(x) | 绝对中位差(median absolute deviation) |
| mad(c(1,2,3,4))返回值为1.48 | |
| quantile(x,probs) | 求分位数。其中x为待求分位数的数值型向量,probs为一个由[0,1]之间的概率值组成 |
| 的数值向量 | |
| # 求x的30%和84%分位点 | |
| y <- quantile(x, c(.3,.84)) | |
| range(x) | 求值域 |
| x <- c(1,2,3,4) | |
| range(x)返回值为c(1,4) | |
| diff(range(x)) | 返回值为3 |
| sum(x) | 求和 |
| sum(c(1,2,3,4))返回值为10 | |
| diff(x, lag=n) | 滞后差分,lag用以指定滞后几项。默认的lag值为1 |
| x<- c(1, 5, 23, 29) | |
| diff(x)返回值为c(4, 18, 6) | |
| min(x) | 求最小值 |
| min(c(1,2,3,4))返回值为1 | |
| max(x) | 求最大值 |
| max(c(1,2,3,4)) | 返回值为4 |
| scale(x,center=TRUE, scale=TRUE) |
为数据对象x按列进行中心化(center=TRUE)或标准化(center=TRUE,scale =TRUE); |
原文地址:http://blog.csdn.net/hongweigg/article/details/45689391