R语言学习笔记(三)——实用的内置函数 |
01内置数据集 934rPO http://blog.numino.net/ R语言中有很多内置的数据集,这些数据集存储在datasets这个包中,包含了R中所有的数据类型。R会默认已经加载了这个包。我们可以用data()命令查看R中所有内置的数据集,左边是数据集的名称,右边是数据集的描述。 f2ZAb0 http://blog.numino.net/ > data()Data sets in package ‘datasets’:AirPassengers Monthly Airline Passenger Numbers 1949-1960BJsales Sales Data with Leading IndicatorBJsales.lead (BJsales) Sales Data with Leading IndicatorBOD Biochemical Oxygen Demand kbCTQ1 http://blog.numino.net/ 这里介绍几个十分常用的数据集,mtcars收集了美国32种汽车的11个指标,经常用于ggplot2作图的示例数据;iris是一个非常有名的数据集,收集了3种鸢尾花花瓣和花萼的长宽信息,是数据挖掘中常用的示例文件;state.x77收集了美国50个州的基本信息,是画热图的示例文件。 3VMyXY http://blog.numino.net/ 02内置函数 607s9o http://blog.numino.net/ R中有很多实用的内置函数,这里不做系统的介绍,仅列举一些在数据分析中非常实用的函数。 3594G7 http://blog.numino.net/ (1)关于NA PSC667 http://blog.numino.net/ 对于一组数据来说,如果出现了缺失值,在R中是不能直接进行统计分析的,好在R中有专门针对缺失值的几个函数,下面举例说明。 FKkEpd http://blog.numino.net/ > x <- c(1, 2, 3, 4, 4, NA)> mean(x)[1] NA 13sp1u http://blog.numino.net/ 向量x中有NA值,看到直接对向量x求平均值结果是不对的,有两种方法可以解决。 2Gj2Pi http://blog.numino.net/ > mean(x, na.rm = T)[1] 2.8> mean(na.omit(x))[1] 2.8 9LO5Uz http://blog.numino.net/ 第一种是在mean函数中添加na.rm = T参数,也就是计算时先将NA值移除;第二种是先用na.omit()函数移除掉x中的NA值,再计算平均值。 9qbAFo http://blog.numino.net/ (2)两个向量的关系 YR1I66 http://blog.numino.net/ 数据分析中经常需要找不同组之间的关系,这里有几个常用的函数。 F8oo34 http://blog.numino.net/ 交集(intersect) qMX6xJ http://blog.numino.net/ > x1 <- c(1:5)> x2 <- c(3:7)> intersect(x1, x2)[1] 3 4 5 4QHLJb http://blog.numino.net/ 并集(union) OyA72h http://blog.numino.net/ > union(x1, x2)[1] 1 2 3 4 5 6 7 vyu40n http://blog.numino.net/ 匹配(match):返回向量1的元素在向量2中的位置 xg8XFt http://blog.numino.net/ > match(x1, x2)[1] NA NA 1 2 3 YM1LT3 http://blog.numino.net/ 表示x1的5个元素在x2中的位置分别为NA、NA、1、2、3。这个函数的意义在于生成一个索引向量,在数据框的筛选中会非常实用。 DYFlmk http://blog.numino.net/ (3)基本统计函数 GkOxdI http://blog.numino.net/ 关于基本的统计函数如平均值、标准差等这里不做系统介绍,这里说几个统计函数的特殊用法。 9z40wR http://blog.numino.net/ (a)生成随机数 5eE3G7 http://blog.numino.net/ 通常用runif(n, min, max)函数,这个函数生成均匀分布的值,n为个数,min和max分别是最小值和最大值,默认参数为0和1。 aaO2Vo http://blog.numino.net/ > runif(5, 1, 10)[1] 7.236265 7.278368 2.847278 9.544132 6.884813> runif(5)[1] 0.6437805 0.2144422 0.4272817 0.6258470 0.6981409 026z8p http://blog.numino.net/ (b)生成随机整数 1JErKh http://blog.numino.net/ 一个最简单的办法是round()和runif()函数连用,其中round()是按照四舍五入取整函数。如果要向上或者向下取整,可以使用ceiling()和floor()函数。 1sK101 http://blog.numino.net/ > round(0.5)[1] 0> round(1.2)[1] 1> round(runif(10)) #生成随机的0,1向量 [1] 0 0 0 1 1 1 1 1 0 0> round(runif(10, 0, 2)) #生成随机的0,1,2向量 [1] 0 1 0 1 2 0 2 0 2 1> ceiling(runif(10, -2, 1)) #生成随机的-1,0,1向量 [1] 0 -1 1 -1 0 -1 0 -1 1 0> floor(runif(10, -1, 2)) [1] 1 0 1 1 -1 1 -1 -1 1 -1 Qc7oOc http://blog.numino.net/ 后面几种生成随机向量的方式非常实用,原理就是先用runif()函数生成随机数,然后再用round()函数取整数。基因型数据经常用0,1,2或者-1,0,1表示,我们可以用这种方式模拟基因型数据。 Ag4Xav http://blog.numino.net/ (c)正态分布 fpl4wI http://blog.numino.net/ 用的最多的是rnorm(n, mean, sd)函数,生成n个符合某个正态分布的随机数。用法比较简单,默认的平均值是0,方差是1。 f2Abi5 http://blog.numino.net/ > rnorm(5)[1] -1.7916223 1.7582387 0.2924384 0.3130404 -0.5076251> rnorm(5, 3, 1)[1] 1.968562 4.885262 4.634220 3.017137 3.990052 pxBb7x http://blog.numino.net/ (4) 其他函数 AT0M92 http://blog.numino.net/ R中还有不少常用的函数,这里仅列举三个。 7wDYnb http://blog.numino.net/ (a)summary函数 N599l3 http://blog.numino.net/ 这个函数是一个比较“万金油”的函数,可以单独对向量或者矩阵使用,会给出一些基本的统计量,包括极值、中位数、平均数等等。 ThqOC6 http://blog.numino.net/ > summary(1:10) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 3.25 5.50 5.50 7.75 10.00> summary(matrix(10:15, nrow = 2)) V1 V2 V3 Min. :10.00 Min. :12.00 Min. :14.00 1st Qu.:10.25 1st Qu.:12.25 1st Qu.:14.25 Median :10.50 Median :12.50 Median :14.50 Mean :10.50 Mean :12.50 Mean :14.50 3rd Qu.:10.75 3rd Qu.:12.75 3rd Qu.:14.75 Max. :11.00 Max. :13.00 Max. :15.00 jq2vd2 http://blog.numino.net/ 在一些统计分析中也经常使用summary()函数,比如下面的线性回归: xQBSH3 http://blog.numino.net/ > x <- c(2, 2, 3, 4, 4)> y <- c(10, 20, 30, 40, 50)> fm <- lm(y ~ x)> summary((fm)) 947xwT http://blog.numino.net/ Call: xQ61g9 http://blog.numino.net/ lm(formula = y ~ x) ymzmBM http://blog.numino.net/ Residuals: 5yIDBE http://blog.numino.net/ 1 2 3 4 5 -5.000e+00 5.000e+00 -1.776e-15 -5.000e+00 5.000e+00 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -15.000 9.037 -1.660 0.1955 x 15.000 2.887 5.196 0.0138 *---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 5.774 on 3 degrees of freedomMultiple R-squared: 0.9, Adjusted R-squared: 0.8667 F-statistic: 27 on 1 and 3 DF, p-value: 0.01385 h0ZWs6 http://blog.numino.net/ 可以看到summary()函数给出了很多统计量,包括残差、截距、显著性等等。 1f2iQq http://blog.numino.net/ (b)table函数 OH3vx8 http://blog.numino.net/ 这个函数看起来不起眼,但很实用,给出一个频率分布表,还是举例说明。 7NPhmq http://blog.numino.net/ > a <- c(rep(NA, 3), rep(1:3, 2))> a[1] NA NA NA 1 2 3 1 2 3> table(a)a1 2 3 2 2 2 KK4p61 http://blog.numino.net/ 上面给出了向量a的频率分布,但没有包含NA,如果要统计NA的数目,则需要添加参数exclude = NULL。 t4Nkr7 http://blog.numino.net/ > table(a, exclude = NULL)a 1 2 3 <NA> 2 2 2 3 LE1IoH http://blog.numino.net/ 需要注意的是生成的频率分布也是一张表,如果需要对这个表进行操作,最好转化成数据框。 089jxa http://blog.numino.net/ > x <- table(a, exclude = NULL) > class(x)[1] "table"> x <- as.data.frame(x) #将x转换成数据框> class(x)[1] "data.frame" wbXzs0 http://blog.numino.net/ (c)apply/sapply/tapply/mapply U9KCsx http://blog.numino.net/ 这是一个函数家族,其实就是为了代替for循环,简化代码,这里只介绍apply和tapply。 QJCZgw http://blog.numino.net/ apply(x, margin, fun, ...)有3个主要的参数,x通常为数据框,margin参数为1或2, 1表示按行,2表示按列,fun为调用函数。比如按列统计mtcars数据集的平均数。 IOVrnr http://blog.numino.net/ > apply(mtcars, 2, mean) mpg cyl disp hp drat wt qsec 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750 vs am gear carb 0.437500 0.406250 3.687500 2.812500 XPyRQ7 http://blog.numino.net/ tapply(x, index, fun, ...)通过index对数据集x进行分组运算,相当于上一期dplyr包中的group_by操作。如我们需要统计iris数据集中不同品种鸢尾花的平均花萼长度。 0ndf47 http://blog.numino.net/ > head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa> tapply(iris$Sepal.Length, iris$Species, mean) setosa versicolor virginica 5.006 5.936 6.588 YS7S2o http://blog.numino.net/ 如果用dplyr则是: 25r9zP http://blog.numino.net/ > iris %>% group_by(Species) %>% summarise(mean = mean(Sepal.Length)) Fs560r http://blog.numino.net/ 3 自编函数 2BBn3I http://blog.numino.net/ R中虽然有很多内置函数,但具体分析时有时候需要用户自己编写函数。自编函数中有两个非常重要的思想,循环和递归。关于循环相信接触R的人都十分了解,这里只列举两个递归函数的例子。 Gt8k95 http://blog.numino.net/ (1)斐波那契数列 BtHKNN http://blog.numino.net/ 斐波那契数列本身就是用递归定义的F(n) = F(n-1) + F(n-2),因此非常适合用递归函数实现。 X2XTsR http://blog.numino.net/ > fib <- function(n){ #定义函数 if(n==1 | n==2){ return(1) } else{ return(fib(n-1)+fib(n-2)) } } > fib(7) #函数调用 [1] 13 6aerdf http://blog.numino.net/ (2)求最大公约数 mTCzR3 http://blog.numino.net/ gcd <- function(a,b) { if (b == 0) return(a) else return(gcd(b, a %% b)) } > gcd(15, 12) [1] 3 JHR40S http://blog.numino.net/ 可见,递归是一种比较高级的编程思想,灵活运用能够极大的化繁为简。 i34YH6 http://blog.numino.net/ 结语:以上就是小编认为R中比较实用的一些内置函数,能力有限总结的肯定不够完全,欢迎大家和小编一起交流R语言学习心得。
|
|