【1.1】R简介与安装

一 简介

R是一个非常灵活的集合命令行,程序语言,图形界面一体的统计和作图的免费的工具。R是S的方言。S语言是于1976妮娜由Bell 实验室开发的,刚开始并没有统计模型;1933年的时候,Bell 实验室卖掉了S语言;2004年,Isightful从Lucent花2百万美元买了过来,并成为现在的老板。

r

缺点:

  • 40年前的老技术;
  • 不怎么支持动态和3D图像
  • 基于用户的设置完成任务,需要一点点基础
  • 物理内存消耗大
  • 不能解决所有的问题

二 R安装和升级

2.1 windows下安装

下载链接:http://cran.r-project.org/

Download R for Windows->base->Download R 2.15.2 for Windows

直接双击安装即可。

安装好之后,启动R,可以看到R console,此时即可以编写R程序了。

Download R for (Mac) OS X  下载地址:同上

选择R-3.3.0.pkg

R-Studio安装

下载链接:http://www.rstudio.com/

R-Studio是一个IDE,其界面和Matlab有点类似,可以让用户很方便地开发,管理R程序,其官方网站的介绍是:

RStudio IDE is a powerful and productive user interface for R. It’s free and open source, and works great on Windows, Mac, and Linux.

安装R-Studio之前需要事先安装好R,然后直接双击R-Studio的exe文件安装即可。

注意:Rstuido应该跟你安装的R在一个盘里,如果R在E盘,Rstudio应该也在E盘。

2.2 mac下的安装

下载地址:https://www.rstudio.com/products/rstudio/download/

简单的双机即可

安装包之前选择镜像,可以选择最近的,也可以选择最新的包的镜像地址:Austria site,最新得CRAN服务器(the Comprehensive R Archive Network))

2.3 centos7下安装R

在centos 7下安装R软件可以使用两种方式:源码编译安装和Yum在线安装。而R的官网上已经说明了,R已经被EPEL仓库管理着,EPEL是一个汇集了各种附加软件包的项目,所以我们使用yum来安装。

2.3.1 yum的安装

1 通过如下命令安装并启用 EPEL (如果已经安装过,直接执行第二步)

yum install epel-release

2 使用如下命令安装R

yum install R

报错1:

Error: Package: R-core-3.4.3-1.el6.x86_64 (epel)
           Requires: libicuuc.so.42()(64bit)
Error: Package: R-core-3.4.3-1.el6.x86_64 (epel)
           Requires: libicui18n.so.42()(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

解决办法:

Download the lib64icu42-4.2.1-1mdv2010.0.x86_64.rpm from the below link: ftp://ftp.icm.edu.pl/vol/rzm6/linux-centos-vault/7.1.1503/os/x86_64/Packages/libicu-50.1.2-11.el7.x86_64.rpm

Install the RPM:

 sudo rpm -ivh libicu-50.1.2-11.el7.x86_64.rpm

报错2:

warning: /var/cache/yum/x86_64/7/epel/packages/htop-1.0.3-1.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7


The GPG keys listed for the "Extra Packages for Enterprise Linux 7 - x86_64" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.


 Failing package is: htop-1.0.3-1.el6.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

解决办法:

To fix you need to manually download and import the key and update the repo config:

wget https://www.atomicorp.com/RPM-GPG-KEY.atomicorp.txt
mv RPM-GPG-KEY.atomicorp.txt /etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt

Change /etc/yum.repos.d/epel.repo. Lines:

gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt

Need an additional entry and become:

gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
        file:///etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt

2.3.2 下载以后的安装

2.3.1 yum的安装

For R graphics to work, “devel” versions of the X11, JPEG, and PNG libraries must be installed on your Linux server. To determine if they are present, enter the following command:

rpm -aq  |grep devel

下载最新版的R

wget -c https://cran.r-project.org/src/base-prerelease/R-devel_2018-04-11_r74581.tar.gz

解压缩

tar –xzf R-.....
cd R-XXX
./configure

报错:

configure: error: libcurl >= 7.22.0 library and headers are required with support for https

安装libcurl

wget --no-check-certificate https://curl.haxx.se/download/curl-7.47.1.tar.gz
 tar xzvf curl-7.47.1.tar.gz
 cd curl-7.47.1
 ./configure 
 make -j3
 make install

然后进入R的文件夹,接着

./configure

还是报错:configure: error: libcurl >= 7.22.0 library and headers are required with support for https

下载低版本的R

wget -c https://cran.r-project.org/src/base/R-3/R-3.2.5.tar.gz

然后:

tar –xzf R-.....
cd R-XXX
./configure

没有报错了,接着:

make
make check

options(repos = "http://cran.us.r-project.org")

2.4 把R安装到指定的位置:

yum install --installroot=/home/share/software/R R

会报错:

Cannot find a valid baseurl for repo: base/$releasever/x86_64

尝试的去解决(没有结果):

sudo vim  ifcfg-eno2
sudo vim ifcfg-enp0s29f0u2 
sudo vim ifcfg-enp140s0 

把ONBOOT=no,改为ONBOOT=yes
重启网络:service network restart

2.5升级

Windows上cmd升级R语言:

cmd上操作调出R的命令行

D:
cd D:\Program Files\R\R-4.1.1\bin
.\R.exe


install.packages("installr");
require(installr)  #load / install+load installr
updateR()

Linux (RedHat, CentOS)上升级R语言:

sudo yum install R

四 学习R

R 的官方网页拥有大量英文学习资源,还可以参考 http://cran.r-project.org/other-docs.html 上的中文翻译文档。 统计之都 bbs 是一个不错的学习 R 的中文网站, 这个论坛上你可以找到大量学习资料或直接提出问题同大家探讨。

3.1 如何自学

作图函数 example(**) 可以看到具体的参数的意思

命令函数 help(**) 可以看到这个命令怎么用

如果以后看到别人写的R程序,不用胆怯,对于新手来说,主要是有些函数可能之前没有见过,你根据()就能推断前面的必然是一个函数,知道了他是函数,就可以查资料了解他的功能以及用法,这样这个函数你就掌握了

介绍几个网站

3.2 工作目录

getwd() 获得工作目录
setwd() 设置工作目录
source('') 调用r代码,用于调用.r文件
setwd(“/sam/abc”) #()告诉我们这是一个命令函数
dir() 知道你现在工作路径里面的文件
str()  告诉你这个对象(可以是命令或文件)是什么
summary()  告诉你文件是什么
q()  退出R

例如:

> str(lm)
function (formula, data, subset, weights, na.action, method = "qr", 
 model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
 contrasts = NULL, offset, ...) 
> z<-rnorm(100,2,4)
> summary(z)
 Min. 1st Qu. Median Mean 3rd Qu. Max. 
-7.4390 -0.3274 2.1070 1.9380 4.2470 12.4800 
> str(z)
 num [1:100] -2.376 -7.439 3.506 2.085 -0.748 ..

五 报错

5.1 意外的输入 in “hp 

因为输入了R不能识别的标志,一般情况是指你的输入字符为中文字符,R是没法识别出来。

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn