| R语言学习笔记(三)——实用的内置函数 |
01内置数据集 T5Yzpy http://blog.numino.net/ R语言中有很多内置的数据集,这些数据集存储在datasets这个包中,包含了R中所有的数据类型。R会默认已经加载了这个包。我们可以用data()命令查看R中所有内置的数据集,左边是数据集的名称,右边是数据集的描述。 7LA2Am 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 i0Nq2E http://blog.numino.net/ 这里介绍几个十分常用的数据集,mtcars收集了美国32种汽车的11个指标,经常用于ggplot2作图的示例数据;iris是一个非常有名的数据集,收集了3种鸢尾花花瓣和花萼的长宽信息,是数据挖掘中常用的示例文件;state.x77收集了美国50个州的基本信息,是画热图的示例文件。 iuKoUM http://blog.numino.net/ 02内置函数 rox8m8 http://blog.numino.net/ R中有很多实用的内置函数,这里不做系统的介绍,仅列举一些在数据分析中非常实用的函数。 e5ZmS4 http://blog.numino.net/ (1)关于NA EThfox http://blog.numino.net/ 对于一组数据来说,如果出现了缺失值,在R中是不能直接进行统计分析的,好在R中有专门针对缺失值的几个函数,下面举例说明。 wBUO95 http://blog.numino.net/ > x <- c(1, 2, 3, 4, 4, NA)> mean(x)[1] NA DCs738 http://blog.numino.net/ 向量x中有NA值,看到直接对向量x求平均值结果是不对的,有两种方法可以解决。 QFROGf http://blog.numino.net/ > mean(x, na.rm = T)[1] 2.8> mean(na.omit(x))[1] 2.8 sUkmXU http://blog.numino.net/ 第一种是在mean函数中添加na.rm = T参数,也就是计算时先将NA值移除;第二种是先用na.omit()函数移除掉x中的NA值,再计算平均值。 tyOq68 http://blog.numino.net/ (2)两个向量的关系 G7Z3M1 http://blog.numino.net/ 数据分析中经常需要找不同组之间的关系,这里有几个常用的函数。 6QvEYL http://blog.numino.net/ 交集(intersect) b87KBm http://blog.numino.net/ > x1 <- c(1:5)> x2 <- c(3:7)> intersect(x1, x2)[1] 3 4 5 wsWdD2 http://blog.numino.net/ 并集(union) 0n7X9u http://blog.numino.net/ > union(x1, x2)[1] 1 2 3 4 5 6 7 DLzZY2 http://blog.numino.net/ 匹配(match):返回向量1的元素在向量2中的位置 2TpWh7 http://blog.numino.net/ > match(x1, x2)[1] NA NA 1 2 3 8cE3GV http://blog.numino.net/ 表示x1的5个元素在x2中的位置分别为NA、NA、1、2、3。这个函数的意义在于生成一个索引向量,在数据框的筛选中会非常实用。 r7x2z1 http://blog.numino.net/ (3)基本统计函数 JorWeS http://blog.numino.net/ 关于基本的统计函数如平均值、标准差等这里不做系统介绍,这里说几个统计函数的特殊用法。 ojzBic http://blog.numino.net/ (a)生成随机数 S5lAa4 http://blog.numino.net/ 通常用runif(n, min, max)函数,这个函数生成均匀分布的值,n为个数,min和max分别是最小值和最大值,默认参数为0和1。 Xg2T7P 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 137zli http://blog.numino.net/ (b)生成随机整数 nb8G1d http://blog.numino.net/ 一个最简单的办法是round()和runif()函数连用,其中round()是按照四舍五入取整函数。如果要向上或者向下取整,可以使用ceiling()和floor()函数。 HInHl2 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 UER4bp http://blog.numino.net/ 后面几种生成随机向量的方式非常实用,原理就是先用runif()函数生成随机数,然后再用round()函数取整数。基因型数据经常用0,1,2或者-1,0,1表示,我们可以用这种方式模拟基因型数据。 3CiIdl http://blog.numino.net/ (c)正态分布 d3n7L8 http://blog.numino.net/ 用的最多的是rnorm(n, mean, sd)函数,生成n个符合某个正态分布的随机数。用法比较简单,默认的平均值是0,方差是1。 wDIt6d 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 E0mzj9 http://blog.numino.net/ (4) 其他函数 HW3Y7x http://blog.numino.net/ R中还有不少常用的函数,这里仅列举三个。 wh2Vs7 http://blog.numino.net/ (a)summary函数 ydoj4U http://blog.numino.net/ 这个函数是一个比较“万金油”的函数,可以单独对向量或者矩阵使用,会给出一些基本的统计量,包括极值、中位数、平均数等等。 5i5rlI 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 zdcaVZ http://blog.numino.net/ 在一些统计分析中也经常使用summary()函数,比如下面的线性回归: EEC9Zy http://blog.numino.net/ > x <- c(2, 2, 3, 4, 4)> y <- c(10, 20, 30, 40, 50)> fm <- lm(y ~ x)> summary((fm)) VQAu3p http://blog.numino.net/ Call: 68E04a http://blog.numino.net/ lm(formula = y ~ x) 6moOwU http://blog.numino.net/ Residuals: WLm9wD 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 L0Pkup http://blog.numino.net/ 可以看到summary()函数给出了很多统计量,包括残差、截距、显著性等等。 jT18T9 http://blog.numino.net/ (b)table函数 f6tU39 http://blog.numino.net/ 这个函数看起来不起眼,但很实用,给出一个频率分布表,还是举例说明。 6T7DN6 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 genwVD http://blog.numino.net/ 上面给出了向量a的频率分布,但没有包含NA,如果要统计NA的数目,则需要添加参数exclude = NULL。 W7l145 http://blog.numino.net/ > table(a, exclude = NULL)a 1 2 3 <NA> 2 2 2 3 Tn6b1K http://blog.numino.net/ 需要注意的是生成的频率分布也是一张表,如果需要对这个表进行操作,最好转化成数据框。 dMtQGD http://blog.numino.net/ > x <- table(a, exclude = NULL) > class(x)[1] "table"> x <- as.data.frame(x) #将x转换成数据框> class(x)[1] "data.frame" kkJChv http://blog.numino.net/ (c)apply/sapply/tapply/mapply e0vUVN http://blog.numino.net/ 这是一个函数家族,其实就是为了代替for循环,简化代码,这里只介绍apply和tapply。 YGAvH7 http://blog.numino.net/ apply(x, margin, fun, ...)有3个主要的参数,x通常为数据框,margin参数为1或2, 1表示按行,2表示按列,fun为调用函数。比如按列统计mtcars数据集的平均数。 x8tKN3 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 dw1R8H http://blog.numino.net/ tapply(x, index, fun, ...)通过index对数据集x进行分组运算,相当于上一期dplyr包中的group_by操作。如我们需要统计iris数据集中不同品种鸢尾花的平均花萼长度。 7shs8G 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 SDmU1T http://blog.numino.net/ 如果用dplyr则是: 7NMbqL http://blog.numino.net/ > iris %>% group_by(Species) %>% summarise(mean = mean(Sepal.Length)) 4S19b8 http://blog.numino.net/ 3 自编函数 2loZSX http://blog.numino.net/ R中虽然有很多内置函数,但具体分析时有时候需要用户自己编写函数。自编函数中有两个非常重要的思想,循环和递归。关于循环相信接触R的人都十分了解,这里只列举两个递归函数的例子。 y5g3Kq http://blog.numino.net/ (1)斐波那契数列 0V8L0S http://blog.numino.net/ 斐波那契数列本身就是用递归定义的F(n) = F(n-1) + F(n-2),因此非常适合用递归函数实现。 D151TO 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 95J556 http://blog.numino.net/ (2)求最大公约数 1DO6jl http://blog.numino.net/ gcd <- function(a,b) { if (b == 0) return(a) else return(gcd(b, a %% b)) } > gcd(15, 12) [1] 3 ae9abq http://blog.numino.net/ 可见,递归是一种比较高级的编程思想,灵活运用能够极大的化繁为简。 kZx8ns http://blog.numino.net/ 结语:以上就是小编认为R中比较实用的一些内置函数,能力有限总结的肯定不够完全,欢迎大家和小编一起交流R语言学习心得。
|
|