1 初识R
1.1 安装R
R由R Core Team维护,R最新版本发布在官方网站,在官网找到安装文件下载链接,并选择距离最近的镜像网站下载(通常速度会更快)。
1.2 R Gui
启动R语言,在R Gui窗口中有菜单栏、工具栏和R控制台。
evernotecid://6B344BC0-88B6-4BC2-9CCA-1B2113776B68/appyinxiangcom/531972/ENResource/p7180
- R控制台(R console)用于输入指令和查看R系统反馈。
- 输入的指令被称为表达式。
- R解释器读入表达式,并返回结果和错误信息。
- 标准R Gui仅通过菜单实现非常初级的功能,但可以通过开源IDE如Rstudio或R扩展包如rattle丰富图形用户界面。
1.3 更新R
2 工作空间
工作空间是当前的R工作环境,包含了用户自定义对象。
Workplace is your current R working environment and includes any user-defined objects.
- 工作空间管理函数
getwd()
# Find the current working directory (where inputs are found and outputs are sent)
setwd("mydirectory")
# Change the current working directory.
ls() # List all variables in the environment.
list.files() # list all the files in your working directory
rm(x) # Remove x from the environment.
rm(list = ls()) #Remove all variables from the environment.
options() #
q() # Quit R
- 示例
old.dir <- getwd()
dir.create("testdir")
setwd("testdir")
file.create("mytest.R")
file.exists("mytest.R")
file.info("mytest.R")
file.info("mytest.R")$mode
file.rename("mytest.R","mytest2.R")
file.copy("mytest2.R","mytest3.R")
file.path("mytest3.R")
file.path(folder1","folder2") # "folder1/folder2"
dir.create(file.path('testdir2', 'testdir3'), recursive = TRUE)
setwd(old.dir)
3 R语法
3 帮助文件
# 帮助文件首页
help.start()
# Find help for a package
help('PackageName')
# Get help of a particular function,以下两式等价
help('FunctionName')
?'FunctionName'
# Search the help files for a word or phrase,以下两式等价
help.search(‘weighted mean’)
??‘weighted mean’
4 符号
Technically R is an expression language with a very simple syntax. It is case sensitive as are most UNIX based packages, so A and a are different symbols and would refer to different variables.
4.1 命令提示符
# 在提示符prompt之后输入命令
>
4.2 命令分隔符
# Commands are separated either by a semi-colon (‘;’), or by a newline.
x <- 2; y <- 3
4.2 赋值符
# assignment operator
# 左赋值,二元操作符(常用),Alt + "-"
<-
# 右赋值,二元操作符(不常用)
->
# R语句由函数和赋值组成
x <- 5 + 7
y <- x - 3
7 -> z
#
<<-
# 一般不使用"="赋值,而用来传参
4.3 传参符
# '='只是将8传给了log函数的参数x,并没有在内存中创建x对象,打印x会报错
log(x = 8, base = 2)
# 表示在内存中创建x对象并赋值8,然后将x传递给log函数的第一个参数x,这时打印x就会输出x的值为8
log(x <- 8, base = 2)
4.4 注释符
# 这是注释,不会在程序中运行
4.5 帮助符
# 函数帮助
?
??
?ggplot2
4.6 转义符
\
a <- "Hello, world"
b <- 'Hello, world'
# 以上两句完全等价
a <- "You should use \"her\" in this 'passage'."
b <- 'You should use "her" in this \'passage\'.'
# 转义符来表明「特定的引号不是字符串定义结束,而是一个双引号字符」
4.6 链式符
# chaning operator(margin)
%<%
4.6 自动补全机制
# auto-completion:tab键
4.7 命令回收机制
# cycle through previous commands:向上箭头↑,向下箭头↓
4.8 数学运算
运算符是包含一个或者两个参数的无括号的函数。
+ # 加号,一元操作符或者二元操作符
- # 减号,一元操作符或者二元操作符
* # 乘法,二元操作符
/ # 除法,二元操作符
^(**) # 幂运算符,二元操作符
x%%y # 求余,二元操作符
x%/%y # 整除,二元操作符
%*% # 矩阵相乘,二元操作符
%o% # 外积,二元操作符
%x% # Kronecker乘积,二元操作符
# 注:矩阵有点积、内积、外积、元素积之分。
算数运算符 加(正):+ 减(负):- 乘: 除:/ 求幂:^(**) 求余:%% 整除:%/% 矩阵相乘:%% 外积:%o% 元素积:xor(x, y) Kronecker乘积:%x% 匹配:%in%(Ctrl + Shift + I) 逻辑运算符 大于:> 大于等于:>= 小于:< 小于等于:<= 等于:== 不等于:!= 或:|(||,只采用第一个元素) 且:&(&&,只采用第一个元素) 非:! 其他运算符
- pipe:%>%(Ctrl + Shift + M)
- 公式:~
- 序列:’:’
- 指定包公式:’::’
- 列表子集:$
- 索引:[ ]([[ ]])
- 函数调用和分组表达式:({ })
4.9 逻辑判断
< # 小于,二元操作符
<= # 小于等于,二元操作符
> # 大于,二元操作符
>= # 大于等于,二元操作符
== # 等于,二元操作符
!= # 不等于,二元操作符
x|y # 或操作,二元操作符,向量模式
x||y # 或操作,二元操作符,不是向量模式
x&y # 与操作,二元操作符,向量模式
x&&y # 与操作,二元操作符,不是向量模式
! # 一元否操作符
4.10 其他
: # 序列colon,二元操作符
:: # dplyr::select, Use a particular function from a package
%in% # 匹配操作,二元操作符(在模型公式中,表示嵌套)
~ # 用于模型公式,一元操作符或者二元操作符
%X% # 特殊二元操作符,X可以被任意合法的名字替换
@ #
$ # 子集subset
'' #
" " #
() #
[] # 子集subset
[[]]
{} #
5 R表达式
R基本命令由表达式和赋值组成。表达式输出结果而不存储,赋值存储结果而不输出。
Elementary commands consist of either expressions or assignments. If an expression is given as a command, it is evaluated, printed (unless specifically made invisible), and the value is lost. An assignment also evaluates an expression and passes the value to a variable but the result is not automatically printed.
# 表达式,输出结果而不存储
5 + 7
# 赋值,赋值存储结果而不输出
x <- 2
## 在赋值外嵌套括号,既输出结果且存储结果
(x <- 2)