听说你想学R?
先来点八卦
当年学R的时候,并没有现在这么多的书籍,老用户一般都知道丁国徽,因为他翻译了诸如《R导论》之类的文档,属于最早的中文材料了。跟生物狗有关的中文书有两本,一本是《R语言及Bioconductor在基因组分析中的应用》,这本我在2006年的时候看过,被坑大发了,里面逻辑混乱、错误满篇。
以下这段豆瓣书评可以说很好的总结了这本书 https://book.douban.com/review/3017404/#comments:
首先,打开前言,读者们会发现“编写本书,参加这些工作的教师和研究生有15人之多”。当然,名字后面带头衔的最后都在出现在封面了。也就是说由12位”无名的岳武穆“研究生同学造就了3名”有名的岳武穆“之砖家叫兽。
然后,本书共24章,模12为零,当然,这只是一个巧合,只是暗合天命罢了。
最搞笑的就是这本书的编排和审校,十分差,许多小错误,就好像从没有审过一样。
譬如第13页上,对order()函数的介绍,居然有这么句绕口令“x[order(x)]等效于order(x)”,当然其实等效的是 sort(x).
其他的诸如第17页上>=的打印错误,就不一一细表了。
最搞笑的就是只有三页的第4章,在数组、函数的介绍之前,就开始讲“聚集计算”!!!然后再来讲解语言基本的语法。。。。
这种“由深入浅,化简单为复杂,化复杂为天书”之功力,更让吾辈叹服砖家叫兽吞云吐雾之神功。作为一本实例讲解的书,根本就没有提供源码和实例样本,也就说,根本就是没头没脑的演示。有点专业精神好么,好歹也是本标价68的书,当然只有400页草纸, 本科生水平的语言文档翻译。
还有一本是我没见过,但我是不会去看的《R语言与Bioconductor生物信息学应用》,同样我也帮你们找了一段书评,不过没有前面豆瓣那段精彩 https://www.amazon.cn/review/RV9K4EFQI9J5W/ref=cm_cr_dp_title/462-0255613-8683213:
那些写好评的,真怀疑是作者的马甲写的。
这绝对是一本自我炒作,而又一无是处的书。
书的大部分都在讲怎么处理芯片,这年头谁还在用芯片?
谷歌了一下作者高山的背景,看书里介绍写的堪萨斯大学结构生物学中心和康奈尔大学BTI植物研究所博后,看着唬人,仔细看文章列表,根本几乎没有任何一作文章,估计是在实验里用着别人写的代码,给别人打杂分析数据的。然后国外混不下去了,不想当千年博后,就跑回国,骗骗同胞
这本书的内容写着完全以SCI论文为依据,其实说白了就是作者拿着本实验室没有价值的芯片script,放在书里,然后又骗了几个小朋友,用中文注释,之所以没有过多介绍RNA-seq 基因组组装和分析,估计是老板不同意他泄露代码。
劝没有买书的朋友们,不要上当受骗,想学生信,还是好好打好基础,学学统计、编程和生物,靠这种贴点代码出来骗钱的书,怎么可能成为高手
本来我们想从这两本书里抄点代码,做点小事情,结果却是一团糟:
重要的事情说三遍:这两本书不要看,这两本书不要看,这两本书不要看!
经常有人发消息说,很想试试我写的东西,但不会用R,其实R还是蛮好入门的,一方面本身是个交互界面,别一方面R的文档做得很好,多看文档,多练习,就容易上手了。最近Rstudio又搞大事,出了个learnr包,以后轻松就可以把软件文档变成交互式的动态学习文档了,这样入门学习就更方便了。当然这个包才出来,还需要一定的时间积累,才会有更多更好的学习资料出来。说到交互学习,我觉得比报班强多了,坐在电脑前,有点空余时间,就可以自学了,每一步都是自己试验过的,入门体验更好,而这种交互学习,R还有一包,叫swirl。
step 0. 安装包
install.packages("swirl")
step 1. 你好!
加载包,并运行swirl()
函数,然后我们就学习->练习->学习->练习
,并且是互动的,好比调戏siri一般,还把R
给学了。
> require(swirl)
> swirl()
| Welcome to swirl! Please sign in. If you've been here before, use the same
| name as you did then. If you are new, call yourself something unique.
What shall I call you? ygc
首先它会问你怎么称呼,我告诉它我叫ygc
。然后它会问我想干嘛:
| Thanks, ygc. Let's cover a couple of quick housekeeping items before we begin
| our first lesson. First of all, you should know that when you see '...', that
| means you should press Enter when you are done reading and ready to continue.
... <-- That's your cue to press Enter to continue
| Also, when you see 'ANSWER:', the R prompt (>), or when you are asked to
| select from a list, that means it's your turn to enter a response, then press
| Enter to continue.
Select 1, 2, or 3 and press Enter
1: Continue.
2: Proceed.
3: Let's get going!
我选3, 开始吧。
step2. 选课
首先我们要选课,好几个课可以选。
| To begin, you must install a course. I can install a course for you from the
| internet, or I can send you to a web page
| (https://github.com/swirldev/swirl_courses) which will provide course options
| and directions for installing courses yourself. (If you are not connected to
| the internet, type 0 to exit.)
1: R Programming: The basics of programming in R
2: Regression Models: The basics of regression modeling in R
3: Statistical Inference: The basics of statistical inference in R
4: Exploratory Data Analysis: The basics of exploring data in R
5: Don't install anything for me. I'll do it myself.
Selection: 1
|==========================================| 100%
| Course installed successfully!
我选1, R编程。于是它就帮我把课给装了。
step 3. 上课
| Please choose a course, or type 0 to exit swirl.
1: R Programming
2: Take me to the swirl course repository!
Selection: 1
| Please choose a lesson, or type 0 to return to course menu.
1: Basic Building Blocks 2: Workspace and Files
3: Sequences of Numbers 4: Vectors
5: Missing Values 6: Subsetting Vectors
7: Matrices and Data Frames 8: Logic
9: Functions 10: lapply and sapply
11: vapply and tapply 12: Looking at Data
13: Simulation 14: Dates and Times
15: Base Graphics
Selection: 1
| | 0%
那么这个课,有几个章节,涵盖了R编程的各个方面,我们可以从任意章节开始。我选1之后,显示我的进度是0%
,在学习过程中会显示进度,好让我们有打怪练级的快感。
| In this lesson, we will explore some basic building blocks of the R
| programming language.
...
|== | 3%
| If at any point you'd like more information on a particular topic related to
| R, you can type help.start() at the prompt, which will open a menu of
| resources (either within RStudio or your default web browser, depending on
| your setup). Alternatively, a simple web search often yields the answer
| you're looking for.
...
|==== | 5%
| In its simplest form, R can be used as an interactive calculator. Type 5 + 7
| and press Enter.
> 5 + 7
[1] 12
| That's a job well done!
从0开始教学,首先R是个交互的计算器,介绍之后,让你自己试一下。你对了,他会说你干得漂亮。
然后说R是个编程语言,我们要存变量。
|====== | 8%
| R simply prints the result of 12 by default. However, R is a programming
| language and often the reason we use a programming language as opposed to a
| calculator is to automate some process or avoid unnecessary repetition.
...
|======= | 11%
| In this case, we may want to use our result from above in a second
| calculation. Instead of retyping 5 + 7 every time we need it, we can just
| create a new variable that stores the result.
...
|========= | 13%
| The way you assign a value to a variable in R is by using the assignment
| operator, which is just a 'less than' symbol followed by a 'minus' sign. It
| looks like this: <-
...
|=========== | 16%
| Think of the assignment operator as an arrow. You are assigning the value on
| the right side of the arrow to the variable name on the left side of the
| arrow.
...
|============= | 18%
| To assign the result of 5 + 7 to a new variable called x, you type x <- 5 +
| 7. This can be read as 'x gets 5 plus 7'. Give it a try now.
> x <- 5+ 7
| Keep up the great work!
step 4. 其实我只想分段, 啦啦啦~
我们用零星的时间,看几眼文档,交互试一下代码。随时可以中断,只要对它说bye()
就好了。
> bye()
| Leaving swirl now. Type swirl() to resume.
我们再次打开swirl()
,还是会问你叫什么,因为同一台机器上的R,可以支持不同的人在上面学习,大家互不干扰。
> swirl()
| Welcome to swirl! Please sign in. If you've been here before, use the same
| name as you did then. If you are new, call yourself something unique.
What shall I call you? ygc
再次打入我的名字,它会问我,是继续学习呢,还是学点新东西。
| Would you like to continue with one of these lessons?
1: R Programming Basic Building Blocks
2: No. Let me start something new.
我想看别的章节,所以选2.
Selection: 2
| Please choose a course, or type 0 to exit swirl.
1: R Programming
2: Take me to the swirl course repository!
Selection: 1
| Please choose a lesson, or type 0 to return to course menu.
1: Basic Building Blocks 2: Workspace and Files
3: Sequences of Numbers 4: Vectors
5: Missing Values 6: Subsetting Vectors
7: Matrices and Data Frames 8: Logic
9: Functions 10: lapply and sapply
11: vapply and tapply 12: Looking at Data
13: Simulation 14: Dates and Times
15: Base Graphics
我这次选9,我想学函数。
Selection: 9
| | 0%
| Functions are one of the fundamental building blocks of the R language. They
| are small pieces of reusable code that can be treated like any other R
| object.
...
|= | 2%
| If you've worked through any other part of this course, you've probably used
| some functions already. Functions are usually characterized by the name of
| the function followed by parentheses.
...
|=== | 4%
| Let's try using a few basic functions just for fun. The Sys.Date() function
| returns a string representing today's date. Type Sys.Date() below and see
| what happens.
>
> Sys.Date()
[1] "2017-01-05"
| All that practice is paying off!
|==== | 6%
| Most functions in R return a value. Functions like Sys.Date() return a value
| based on your computer's environment, while other functions manipulate input
| data in order to compute a return value.
...
|====== | 8%
| The mean() function takes a vector of numbers as input, and returns the
| average of all of the numbers in the input vector. Inputs to functions are
| often called arguments. Providing arguments to a function is also sometimes
| called passing arguments to that function. Arguments you want to pass to a
| function go inside the function's parentheses. Try passing the argument c(2,
| 4, 5) to the mean() function.
> mean(c(2,4,5))
[1] 3.666667
| Excellent job!
|======= | 10%
| Functions usually take arguments which are variables that the function
| operates on. For example, the mean() function takes a vector as an argument,
| like in the case of mean(c(2,6,8)). The mean() function then adds up all of
| the numbers in the vector and divides that sum by the length of the vector.
...
很长的一段拷贝下来,我懒得一段一段写了,你稍微看一下,就知道了,它从函数调用,函数返回值,函数参数开始一点一点给你展开,等你学完函数这一章,你也能自己写函数了。
除了R编程基础,还有很多别的课程,比如《数据分析》、《数据清洗》、《回归模型》和《统计推断》等。
在R里,利用零散的空闲时间,就可以一点一滴地把R给学起来。你还在等什么!赶快玩起来。