上一篇文章是R语言的入门。涉及了一些最最基础的知识,主要包括R里的几种数据类型。建议R语言零基础的同学把第一讲理解并掌握后,再进行这一讲的学习。这一部分主要涉及了R语言中的循环,不仅讲解了if, else if, while,for,还讲解了一些function,例如apply()(之前总能在很多大神的教程中看到这个function,但是一直不理解究竟是怎么work的,现在总算明白了)。另外讲到了sapply()这个function也很有意思。
视频地址:https://www.youtube.com/watch?v=GMx8A-p5L4E
网页版PPT地址:https://rockefelleruniversity.github.io/Intro_To_R_1Day/presentations/slides/introToR_Session2.html#1
![](https://img.haomeiwen.com/i18922188/d0e361c09d76f661.jpg)
![](https://img.haomeiwen.com/i18922188/a77f57a7defbdb74.jpg)
这里原本还有几页PPT,但都是对第一讲的回顾,很多内容重复,所以我就删掉了。
![](https://img.haomeiwen.com/i18922188/5ef4ff29cdd918c9.jpg)
![](https://img.haomeiwen.com/i18922188/aef89379667f3f9a.jpg)
![](https://img.haomeiwen.com/i18922188/f2b2e249b4dc0baf.jpg)
![](https://img.haomeiwen.com/i18922188/079e20b370a6a067.jpg)
NOTE: if()
语句先判断()里的内容是不是符合逻辑上的TRUE,如果符合,则执行{}里的命令;如果在逻辑上是FALSE,则不执行{}里的命令。所以上面PPT里,当x是TRUE时,执行了message这行命令,输出的是x is true。而当x是FALSE的时候,则没有输出。
![](https://img.haomeiwen.com/i18922188/c002307c6a7cfce2.jpg)
NOTE:但是多数情况下,无论if()里是TRUE还是FALSE,我们总是希望能得到输出,你希望知道what's going on。那么你可以用else语句:
![](https://img.haomeiwen.com/i18922188/64542f4d2a78189d.jpg)
NOTE:需要注意的是else是在第一个{}之后马上出现的,不用换行,并且else之后马上跟着第二个{},也不用换行。
![](https://img.haomeiwen.com/i18922188/b08df86747275e2b.jpg)
NOTE:如果你的判断条件多余2种可能,那么你需要else if。你可以添加多个else if。
![](https://img.haomeiwen.com/i18922188/aa816f7ff4b2e21a.jpg)
![](https://img.haomeiwen.com/i18922188/b94272483e153ce6.jpg)
![](https://img.haomeiwen.com/i18922188/5a6a7c8c45ff1fd5.jpg)
NOTE:else if语句是判断语句。那么接下来这个就是loops(循环),你可以限制某个命令被执行多少次:
![](https://img.haomeiwen.com/i18922188/f5b9c135e9d7017f.jpg)
![](https://img.haomeiwen.com/i18922188/d358c434f06e7b6c.jpg)
NOTE: appendLF
: logical: should messages given as a character string have a newline appended?如果是F,则输出的字符在同一行;如果是T,则每个字符单独占一行。
toupper
: Changes the case to upper.变成大写。
![](https://img.haomeiwen.com/i18922188/e50e99ad58a554e4.jpg)
![](https://img.haomeiwen.com/i18922188/b923ca9cf1a73426.jpg)
![](https://img.haomeiwen.com/i18922188/6aee07a1d79761b6.jpg)
![](https://img.haomeiwen.com/i18922188/21a394ea8026ed06.jpg)
NOTE: apply
也是一种循环。可对某一个命令执行多次。这个function是上面三种里最常用的一种。
![](https://img.haomeiwen.com/i18922188/4dea76d2e7cc9d84.jpg)
![](https://img.haomeiwen.com/i18922188/8eb49b892deb496a.jpg)
![](https://img.haomeiwen.com/i18922188/b067aa4b05583e61.jpg)
NOTE:apply()
里的第二个参数是行/列,1代表行,2代表列,所以上面PPT的意思就是把matexample里所有行”打印“出来。后面的collapse的功能就是把你指定的行或列里的元素放在一起,作为一个character,每个元素用分号隔开。所以最后输出的都是用“”括起来的。
![](https://img.haomeiwen.com/i18922188/a7d3b57d04217f7f.jpg)
![](https://img.haomeiwen.com/i18922188/304e8f8e036aa2e8.jpg)
![](https://img.haomeiwen.com/i18922188/b9a6bed0bc4bf70e.jpg)
![](https://img.haomeiwen.com/i18922188/ec4db3ec4bdeca2a.jpg)
![](https://img.haomeiwen.com/i18922188/9d8d80215d508205.jpg)
![](https://img.haomeiwen.com/i18922188/9fdcc1c392abf710.jpg)
![](https://img.haomeiwen.com/i18922188/c661f0d7229ee8dc.jpg)
![](https://img.haomeiwen.com/i18922188/bbba8634b3512a49.jpg)
![](https://img.haomeiwen.com/i18922188/9aff6e31728aa5d1.jpg)
![](https://img.haomeiwen.com/i18922188/4006ddb27510bded.jpg)
![](https://img.haomeiwen.com/i18922188/4002e034fd4af03a.jpg)
![](https://img.haomeiwen.com/i18922188/8c784bed80cfc0d8.jpg)
![](https://img.haomeiwen.com/i18922188/e9f565dece356775.jpg)
![](https://img.haomeiwen.com/i18922188/67f41eb717e90262.jpg)
![](https://img.haomeiwen.com/i18922188/599910d9cc666a55.jpg)
![](https://img.haomeiwen.com/i18922188/1bffef40174ef247.jpg)
![](https://img.haomeiwen.com/i18922188/e265a5b64825d4cb.jpg)
![](https://img.haomeiwen.com/i18922188/d5b69f7f5dcb97c9.jpg)
NOTE:在全局环境里定义的参数数值,不会在function里的重新定义而改变。除非在function()里重新定义的时候使用“<<-”代替“<-”。
![](https://img.haomeiwen.com/i18922188/9258e0fc53460dbb.jpg)
![](https://img.haomeiwen.com/i18922188/684545a1921e5aa9.jpg)
![](https://img.haomeiwen.com/i18922188/f42a38d85a1746f2.jpg)
![](https://img.haomeiwen.com/i18922188/b312e6f40fff958c.jpg)
![](https://img.haomeiwen.com/i18922188/7dd34abf468a05e7.jpg)
![](https://img.haomeiwen.com/i18922188/2b401098447a2ab9.jpg)
![](https://img.haomeiwen.com/i18922188/44ee961a3c3a8819.jpg)
![](https://img.haomeiwen.com/i18922188/0758b7ef4d099366.jpg)
![](https://img.haomeiwen.com/i18922188/b099d0b1f3ed1ba1.jpg)
![](https://img.haomeiwen.com/i18922188/edee7e6a84f4ab1e.jpg)
![](https://img.haomeiwen.com/i18922188/65c0380cc6f92407.jpg)
![](https://img.haomeiwen.com/i18922188/b319e1c1ad022a11.jpg)
![](https://img.haomeiwen.com/i18922188/0c98e3ec425e77c3.jpg)
![](https://img.haomeiwen.com/i18922188/54b7b4fcee4aa1d5.jpg)
![](https://img.haomeiwen.com/i18922188/335e493127508ae5.jpg)
【练习时间】https://rockefelleruniversity.github.io/Intro_To_R_1Day//exercises/exercises/Functions_exercise.html
![](https://img.haomeiwen.com/i18922188/f7fab3cc6bd1bbf1.jpg)
【答案】https://rockefelleruniversity.github.io/Intro_To_R_1Day//exercises/answers/Functions_answers.html
网友评论