R/R语言简介

维基教科书,自由的教学读本
< R

R语言的起源历史[编辑]

R语言是S语言的一种方言,是“GNU S”, 一个自由的、有效的、用于统计计算和绘图的语言和环境,它提供了广泛的统计分析和绘图技术:包括线性和非线性模型、统计检验、时间序列、分类、聚类等方法。

什么是S[编辑]

S语言是由Bell实验室的John Chambers等人开发的。 S语言1976年开始作为内部统计分析环境--最初作为Fortran库的实现。 最初语言版本不包括统计模型函数。 1988年该系统用C语言重新编写,与我们现在看到的系统类似(这是该语言的第3版)。Chambers和Hastie的《Statistical Models in S》(白皮书)描述了S语言的统计分析功能。 S语言的第4版在1988年发行,这就是我们今天所用的版本。John Chambers的《Programming with Data》(绿皮书)描述这个版本的S语言。

S历史大事纪[编辑]

1993年,Bell实验室授权StatSci(现在的Insightful Corp.)专用的license来开发和出售S语言。 2004年,Insightful花费200万美元从Lucent购买了S语言,是S语言现在的所有者。 2006年,Alcatel购买了Lucent Technologies,并改名为Alcatel-Lucent。 Insightful销售基于S语言的产品S-PLUS,S-PLUS在S语言基础建立许多可视化的特征,因此称为“PLUS”。 2008年,TIBCO以2500万美元控股Insightful。 S语言自从1998年后没有过重大更新。 1998年,S语言赢得了Association for Computing Machinery’s Software System Award。

S哲学[编辑]

在“S进化的阶段”中,John Chanbers写到:“我们希望用户能够在一个交互性的环境,就像他们不认为自己在编程一样。当他们的需求逐渐变得清晰,熟练度逐步提高,他们就可以逐渐地进入编程,这时语言和系统特性会变得更加重要。http://www.stat.bell-labs.com/S/history.html ”

回到R[编辑]

1991:新西兰奥克兰大学的 Ross Ihaka 和 Robert Gentleman 两人共同创建,其词法和语法分别源自 Scheme 和 S 语言。他们开发R的经验被发表在1996的JCGS。 1993:第一个R声明向公众发布。 1995:Martin Mächler说服Ross和Robert使用 GNU General Public License使得称为开源免费软件。 1996:公共邮件系统被创建(R-help和R-devel)。 1997:R Core Group成立(包括部分S-PLUS的人员)。核心小组控制着R源代码。 2000:R 1.0.0发布。 2013:R 3.0.2在12月发布。 2014:R 3.1.2于10月31日发布。 2019:R 3.5.3 (Great Truth)于2019-03-11发布。 2019:R 3.6.0于2019-04-26即将发布.

小结[编辑]

R是S语言的一种实现。S语言是由AT&T贝尔实验开发的一种用来进行数据探索、统计分析、作图的解释型语言。最初S语言的实现版本主要是S-PLUS。 S-PLUS是一个商业软件,它基于S语言,并由MathSoft公司的统计科学不进一步完善。后来Auckland大学的Ross Ihaka 和 Robert Gentleman 及其他志愿者开发了R系统。 R语言的使用和S-PLUS有很多类似之处,两个软件有一定的兼容性。

参考资料[编辑]

https://statstudy.github.io/doc/R_base_1.html https://github.com/jtleek/modules/blob/master/02_RProgramming/OverviewHistoryR/index.md

https://en.wikipedia.org/wiki/R_(programming_language)

R语言定义与特征[编辑]

R是一个开放的统计编程环境,是一门用于统计计算和作图的语言。 R is a language and environment for statistical computing and graphics. http://www.r-project.org/about.html

R特征[编辑]

  • 语法接近S语言,方便S_PLUS用户使用。
  • 语义表面上与S类似,但实际上是不同的。
  • 全平台/系统运行。 (包括PlayStation 3)
  • 定期更新(每年修复bug);开发积极。
  • 极其精简,就像一个软件;函数封装在不同的扩展包中。
  • 绘图能力十分强大,强于大多数统计学软件。
  • 便于交互工作,但包含开发新工具的强大编程语言
  • 积极活跃的用户社区;R-help和R-devel mailing lists、Stack Overflow
  • 免费

免费软件[编辑]

With free software, you are granted

  • The freedom to run the program, for any purpose (freedom 0).
  • The freedom to study how the program works, and adapt it to your needs (freedom 1). Access to the source code is a precondition for this.
  • The freedom to redistribute copies so you can help your neighbor (freedom 2).
  • The freedom to improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this. http://www.fsf.org

R缺点[编辑]

  • 基于40年前的技术开发。
  • 对动态或3D图形支持少(“old days”版本后有所提升)。
  • 函数开发处于用户需求和贡献。如果没人和你有相同需求,那么这可能就是你的工作(或者付费找人开发)。
  • 对象必须储存在物理内存中,但现在有很多新技术可以实现。
  • 不能应对所有可能的情况 (这也是所有软件的弊端)。

R系统的设计[编辑]

R系统分成两个概念模块:

  1. 从CRAN下载的“base” R系统。
  2. 其他。 R函数分别储存在不同的packages中。
  • “base” R系统包括:  base package是运行R所必须的,包含了多多树基础函数。
  • “base”系统中的其他packages:utils, stats, datasets, graphics, grDevices, grid, methods, tools, parallel, compiler, splines, tcltk, stats4
  • 建议安装的packages: boot, class, cluster, codetools, foreign, KernSmooth, lattice, mgcv, nlme, rpart, survival, MASS, spatial, nnet, Matrix.

还有很多其他的扩展包:

  • 14086 CRAN packages.
  • Bioconductor project packages (http://bioconductor.org).
  • 还有一些自己开发的扩展包存放在github或其他代码托管平台,甚至是个人网站上,没有办法可靠统计这些包的数量。

R学习资源[编辑]

在线资料[编辑]

CRAN资料

各种cheatsheet[编辑]

R视频教程[编辑]

【生信技能树】生信人应该这样学R语言

【生信技能树】公共数据库挖掘实例(基于R语言)

R语言入门生信应用基础课程

R语言入门数据分析

生物信息学入门基础讲座的R部分


R书籍[编辑]

bookdown写书

bookdown写的公开书资源

初级入门[编辑]

《An Introduction to R》

《R4Beginners》

153 分钟学会 R

《统计建模与R软件》

《R Cookbook》

R语言实战(第2版)《R in action》

《R in a Nutshell》

高级入门[编辑]

《Statistics with R》

《The R book》

绘图与可视化[编辑]

《R Graphics》

《R Graphics Cookbook》

《Lattice:Multivariate Data Visualization with R》

《ggplot2:Elegant Graphics for Data Analysis》

《Data Mining with Rattle and R》

《Interactive and Dynamic Graphics for Data Analysis With R and GGobi》

《现代统计图形》

时间序列分析[编辑]

《Time Series Analysis and Its Applications :with R examples》

《Analysis of financial time series》

《Analysis of Integrated and Cointegrated Time Series with R》

《Wavelet Methods in Statistics with R》

计量经济学[编辑]

《Econometrics In R》

《Applied Econometrics with R》

金融[编辑]

《Advanced Topics in Analysis of Economic and Financial Data Using R》

《Modelling Financial Time Series With S-plus》

《Simulation Inference Stochastic Differential Equations:with R examples》

《Simulation Techniques in Financial Risk Management》

《Modern Actuarial Risk Theory Using R》

《Quantitative Risk Management:Concepts, Techniques and Tools》

《Portfolio Optimization with R》

《Option Pricing and Estimation of Financial Models with R》。

数据挖掘[编辑]

《Data Mining with R:learing with case studies》

《Data Mining explain using R》

系列图书[编辑]

Springer的Use R!系列图书

R官方书单

《R for Data Science》的中译版(英文版链接

《A Handbook of Statistical Analyses_Using_R》

《Modern Applied Statistics With S》

《Introduction to Scientific Programming and Simulation Using R》

《Mastering Scientific Computing with R》

《Practical Data Science with R》

《software for data analysis programming with R》

生物信息学资源[编辑]

R语言的运行环境[编辑]

CRAN的R语言环境 集成开发环境Rstudio Microsoft R Open 基于anaconda/Miniconda构建的R环境

CRAN的R语言环境[编辑]

官方网站:https://www.r-project.org/

windows下载后,双击安装。

Mac系统自带R,也可以下载安装,和windows类似。

# Ubuntu:
apt-get update
apt-get install r-base r-base-dev
# CentOS
yum install epel-release
yum install R

# 源码编译安装,CentOS7系统测试过,不需要安装依赖
wget https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-3/R-3.5.3.tar.gz
tar zxvf R-3.5.3.tar.gz
cd R-3.5.3
./configure --enable-R-shlib=yes --with-tcltk --prefix=/opt/biosoft/R
make -j 4
sudo make install
# Linux:CentOS 7.5
echo 'export PATH=/opt/biosoft/R:$PATH' >> ~/.bashrc

最好用的R语言IDE--Rstudio[编辑]

Rstudio网站(Free版即可)下载RStudio Desktop Open Source License Windows和mac系统安装比较容易。 Linux服务器RStudio Server Open Source License的,需要进行一些网络的设置,安装好后,可以通过服务器ip地址通过网页使用服务器版的RStudio。

Microsoft R Open[编辑]

基于anaconda/Miniconda构建的R环境[编辑]

软件包下载:windwos和mac都是可视化安装,比较简单。 Linux系统下载安装包后,需要使用命令行命令bash安装:

# 注意
bash Anaconda2-5.3.1-Linux-x86_64.sh
# 设置好安装路径,并同意将路径添加到系统PATH中,否则需要自己添加。
# 安装配置好conda后
conda install -y r

如此,可以开始R之旅了!!!

R语言扩展包[编辑]

utils包中的*.packages函数管理CRAN源的扩展包

BiocManager管理bioconductor中的扩展包

devtools安装开发中扩展包

github_install安装github上的扩展包

utils包中的*.packages函数[编辑]

## 修改CRAN的镜像源(官方的软件源在国外,速度会受影响)
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))

## 列出所有当前可用扩展包
> available.packages()
                              Package                         Version         
A3                            "A3"                            "1.0.0"         
abbyyR                        "abbyyR"                        "0.5.4"         
abc                           "abc"                           "2.1"           
......
## 函数返回一个矩阵形式的扩展包列表,包括软件包名称及当前最新版本

## 安装扩展包
> install.packages("PackageName")

## 查看已安装的软件包
> installed.packages("PackageName")

## 更新软件包
## 更新特定扩展包
> update.packages("PackageName")
## 更新所有软件包
> update.packages()

## 移除扩展包
> remove.packages("PackageName")

BiocManager:管理bioconductor中的扩展包[编辑]

if (!"BiocManager" %in% rownames(installed.packages()))
     install.packages("BiocManager")
## 修改biocondutor的镜像
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")

## 列出所有可以安装的R扩展包
> BiocManager::available()

## 安装扩展包
> BiocManager::install("PackageName")
## 由于许多biocondutor扩展包是依赖CRAN上的扩展包的,因此,BiocManager::install()也可以用来安装CRAN的扩展包
## 同时安装多个扩展包
pkgs <- c("PackageName1", "PackageName2", "PackageName3",...)
BiocManager::install(pkgs)

devtools安装开发中扩展包[编辑]

https://cran.r-project.org/package=devtools devtools包含了一系列用来开发R包的工具。

# install.packages("devtools")
library(devtools)
install_github('hadlley/dplyr')

githubinstall安装github上的扩展包[编辑]

https://cran.r-project.org/package=githubinstall

#install.packages('githubinstall') 
library(githubinstall)
githubinstall('AnomalyDetection')

R扩展包储存的镜像地址[编辑]

R包帮助系统[编辑]

在线帮助系统[编辑]

本地帮助系统[编辑]

#### 关于R的基础知识 ####
help.start()


#### 关于R中的函数或关键字符 ####
## 查看sum函数的帮助文档
help(sum)
?sum
## 查看heatmap函数的帮助文档
help(heatmap)
?heatmap
## 查找所有包含histogram函数的扩展包的帮助文档
help("histogram",try.all.packages=T)
## 查找lattice包中的histogram帮助文档
help("histogram",package="lattice")

## R site search
Search help files, manuals, and mailing list archives.
## 搜索http://search.r-project.org网站的帮助页面
RSiteSearch("matlab")

## 找出名字中含有“plot”的函数
apropos("plot")
apropos("plot", mod="function")
## 列出所有在帮助页面含有字符“plot”的函数帮助文档
help.search("plot")
## 得到名为“plot”函数所在的程序包
find("plot")
## 得到名为“plot”函数的自变量列表
args("plot")

## 展示mean函数使用示例
example(mean)
## 显示hist函数使用示例
example("hist")
## 展示数据处理和绘图实例
example("example")
## 展示R语言的各种类型的绘图示例
demo(graphics)
## R 3D绘图示例
demo(persp)

## 查看Biostrings软件包自带的帮助文档
browseVignettes(package = "Biostrings")
system.file(package = "Biostrings")
## 将软件包Biostrings中Rnw格式转换成纯R代码格式
library("tools")
vigSrc = list.files(pattern = "Rnw$",
                    system.file("doc", package = "Biostrings"),
                    full.names = TRUE)
vigSrc
for (v in vigSrc) Stangle(v)