| R语言学习笔记(三)——实用的内置函数 |
01内置数据集 Vi58Z8 http://blog.numino.net/ R语言中有很多内置的数据集,这些数据集存储在datasets这个包中,包含了R中所有的数据类型。R会默认已经加载了这个包。我们可以用data()命令查看R中所有内置的数据集,左边是数据集的名称,右边是数据集的描述。 kYQ2BY 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 T799dk http://blog.numino.net/ 这里介绍几个十分常用的数据集,mtcars收集了美国32种汽车的11个指标,经常用于ggplot2作图的示例数据;iris是一个非常有名的数据集,收集了3种鸢尾花花瓣和花萼的长宽信息,是数据挖掘中常用的示例文件;state.x77收集了美国50个州的基本信息,是画热图的示例文件。 9OFQz3 http://blog.numino.net/ 02内置函数 sw27mz http://blog.numino.net/ R中有很多实用的内置函数,这里不做系统的介绍,仅列举一些在数据分析中非常实用的函数。 weC261 http://blog.numino.net/ (1)关于NA fQ46At http://blog.numino.net/ 对于一组数据来说,如果出现了缺失值,在R中是不能直接进行统计分析的,好在R中有专门针对缺失值的几个函数,下面举例说明。 MZ5HGY http://blog.numino.net/ > x <- c(1, 2, 3, 4, 4, NA)> mean(x)[1] NA 9Vqe2j http://blog.numino.net/ 向量x中有NA值,看到直接对向量x求平均值结果是不对的,有两种方法可以解决。 Ym7ddy http://blog.numino.net/ > mean(x, na.rm = T)[1] 2.8> mean(na.omit(x))[1] 2.8 gv1aF1 http://blog.numino.net/ 第一种是在mean函数中添加na.rm = T参数,也就是计算时先将NA值移除;第二种是先用na.omit()函数移除掉x中的NA值,再计算平均值。 p7tFoJ http://blog.numino.net/ (2)两个向量的关系 QutfNx http://blog.numino.net/ 数据分析中经常需要找不同组之间的关系,这里有几个常用的函数。 Y5qtFJ http://blog.numino.net/ 交集(intersect) zxf7Go http://blog.numino.net/ > x1 <- c(1:5)> x2 <- c(3:7)> intersect(x1, x2)[1] 3 4 5 G4I63n http://blog.numino.net/ 并集(union) 016vUS http://blog.numino.net/ > union(x1, x2)[1] 1 2 3 4 5 6 7 48YmId http://blog.numino.net/ 匹配(match):返回向量1的元素在向量2中的位置 p7rO8m http://blog.numino.net/ > match(x1, x2)[1] NA NA 1 2 3 3Q2iyR http://blog.numino.net/ 表示x1的5个元素在x2中的位置分别为NA、NA、1、2、3。这个函数的意义在于生成一个索引向量,在数据框的筛选中会非常实用。 E4i2E9 http://blog.numino.net/ (3)基本统计函数 RiUT7n http://blog.numino.net/ 关于基本的统计函数如平均值、标准差等这里不做系统介绍,这里说几个统计函数的特殊用法。 y0LiVH http://blog.numino.net/ (a)生成随机数 wf1EzP http://blog.numino.net/ 通常用runif(n, min, max)函数,这个函数生成均匀分布的值,n为个数,min和max分别是最小值和最大值,默认参数为0和1。 37N4KP 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 yXGNO1 http://blog.numino.net/ (b)生成随机整数 i3djwH http://blog.numino.net/ 一个最简单的办法是round()和runif()函数连用,其中round()是按照四舍五入取整函数。如果要向上或者向下取整,可以使用ceiling()和floor()函数。 M1bhr0 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 0rUrgo http://blog.numino.net/ 后面几种生成随机向量的方式非常实用,原理就是先用runif()函数生成随机数,然后再用round()函数取整数。基因型数据经常用0,1,2或者-1,0,1表示,我们可以用这种方式模拟基因型数据。 Xtm6nq http://blog.numino.net/ (c)正态分布 PW12MD http://blog.numino.net/ 用的最多的是rnorm(n, mean, sd)函数,生成n个符合某个正态分布的随机数。用法比较简单,默认的平均值是0,方差是1。 icVGNE 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 S7Au6n http://blog.numino.net/ (4) 其他函数 MrpPlK http://blog.numino.net/ R中还有不少常用的函数,这里仅列举三个。 pZ9HDl http://blog.numino.net/ (a)summary函数 HCUNSj http://blog.numino.net/ 这个函数是一个比较“万金油”的函数,可以单独对向量或者矩阵使用,会给出一些基本的统计量,包括极值、中位数、平均数等等。 p2nu4Q 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 5Ze0pk http://blog.numino.net/ 在一些统计分析中也经常使用summary()函数,比如下面的线性回归: 5KHess http://blog.numino.net/ > x <- c(2, 2, 3, 4, 4)> y <- c(10, 20, 30, 40, 50)> fm <- lm(y ~ x)> summary((fm)) BuvPaN http://blog.numino.net/ Call: c0mUyE http://blog.numino.net/ lm(formula = y ~ x) CD9Nng http://blog.numino.net/ Residuals: TJI1eu 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 pkQM4O http://blog.numino.net/ 可以看到summary()函数给出了很多统计量,包括残差、截距、显著性等等。 7GqR2x http://blog.numino.net/ (b)table函数 288gMu http://blog.numino.net/ 这个函数看起来不起眼,但很实用,给出一个频率分布表,还是举例说明。 ryrRZs 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 VkBXPZ http://blog.numino.net/ 上面给出了向量a的频率分布,但没有包含NA,如果要统计NA的数目,则需要添加参数exclude = NULL。 m91D6U http://blog.numino.net/ > table(a, exclude = NULL)a 1 2 3 <NA> 2 2 2 3 LR46Tk http://blog.numino.net/ 需要注意的是生成的频率分布也是一张表,如果需要对这个表进行操作,最好转化成数据框。 fVGXgq http://blog.numino.net/ > x <- table(a, exclude = NULL) > class(x)[1] "table"> x <- as.data.frame(x) #将x转换成数据框> class(x)[1] "data.frame" 6Nf4Lr http://blog.numino.net/ (c)apply/sapply/tapply/mapply A8UfRW http://blog.numino.net/ 这是一个函数家族,其实就是为了代替for循环,简化代码,这里只介绍apply和tapply。 9vKt8e http://blog.numino.net/ apply(x, margin, fun, ...)有3个主要的参数,x通常为数据框,margin参数为1或2, 1表示按行,2表示按列,fun为调用函数。比如按列统计mtcars数据集的平均数。 FpjI55 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 ximN1D http://blog.numino.net/ tapply(x, index, fun, ...)通过index对数据集x进行分组运算,相当于上一期dplyr包中的group_by操作。如我们需要统计iris数据集中不同品种鸢尾花的平均花萼长度。 4bYO97 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 xSZwPO http://blog.numino.net/ 如果用dplyr则是: Qq9qDO http://blog.numino.net/ > iris %>% group_by(Species) %>% summarise(mean = mean(Sepal.Length)) XpmEzu http://blog.numino.net/ 3 自编函数 6mw746 http://blog.numino.net/ R中虽然有很多内置函数,但具体分析时有时候需要用户自己编写函数。自编函数中有两个非常重要的思想,循环和递归。关于循环相信接触R的人都十分了解,这里只列举两个递归函数的例子。 psqtGP http://blog.numino.net/ (1)斐波那契数列 1CAckX http://blog.numino.net/ 斐波那契数列本身就是用递归定义的F(n) = F(n-1) + F(n-2),因此非常适合用递归函数实现。 J3Pu9H 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 7h84c0 http://blog.numino.net/ (2)求最大公约数 42sb1l http://blog.numino.net/ gcd <- function(a,b) { if (b == 0) return(a) else return(gcd(b, a %% b)) } > gcd(15, 12) [1] 3 pfSgC9 http://blog.numino.net/ 可见,递归是一种比较高级的编程思想,灵活运用能够极大的化繁为简。 fdGoF2 http://blog.numino.net/ 结语:以上就是小编认为R中比较实用的一些内置函数,能力有限总结的肯定不够完全,欢迎大家和小编一起交流R语言学习心得。
|
|