变量与工作空间

1. 变量

变量命名

R语言的变量可以存储原子向量、原子向量组或许多R对象的组合。

命名规范:

  • 变量名由字母、数字、下划线或点 . 组成

  • 变量名的开头:

    • 以字母开头,或者以点开头(但点后面不能紧跟数字)

    • 不能以数字开头,不能以下划线开头

定义变量

无需指定变量类型,直接取名使用即可,例如:

num_1=c(1,2)  #函数c()把一系列的数据拼接起来,创建向量
str.hello="hello world"
b=TRUE

变量赋值

  • 赋值:可以用 向左 <- 、向右-> 、等号 = 来为变量分配值。

  • 打印变量值:用 print()或者 cat(),不同的是,前一个只能打印一个变量,后一个能打印多个变量,如:(注意两者输出的格式也不一样)

a <- c(1,2,3)
b <- c(4,5,6)
print(a)
## [1] 1 2 3
cat(a,b)
## 1 2 3 4 5 6

查找变量

利用ls()查看已经定义的变量

print(ls( ))
## [1] "a"         "b"         "num_1"     "str.hello"

使用模式 pattern来匹配含有指定字符的变量

print(ls(pattern = 'a'))   #注意,函数内的参数不能用 '<-' 和 '->'来赋值
## [1] "a"

删除变量

使用rm()函数对当前工作空间内的变量进行删除

a <- c(1,2,3)
b <- c(4,5,6)
ab <- c(7,8,9)
print(ls())
## [1] "a"         "ab"        "b"         "num_1"     "str.hello"
rm(ab) #删除一个变量ab
print(ls())
## [1] "a"         "b"         "num_1"     "str.hello"
rm(a,b) #删除多个变量a,b
print(ls())
## [1] "num_1"     "str.hello"

删除所有的变量可以用语句 rm(list= ls())

rm(list = ls())
print(ls()) #输出为character(0)
## character(0)

2. 运算符

算数运算符

R语言的算数运算符包括+-*/%%%/%^,并且可以对向量进行运算,例如:

print(c(1,2) + c(3,4))    #+:两个向量相加
## [1] 4 6
print(c(1,2) - c(3,4))    #-:两个向量相减
## [1] -2 -2
print(c(1,2) * c(3,4))    #*:两个向量相乘
## [1] 3 8
print(c(3,2) / c(3,4))    #/:两个向量相除
## [1] 1.0 0.5
print(c(5,8) %% c(3,4))   #%%:两个向量求余
## [1] 2 0
print(c(5,8) %/% c(3,4))  #%/%:两个向量求商
## [1] 1 2
print(c(2,2) ^ c(3,4))    #^:乘方运算
## [1]  8 16

关系运算符

R语言关系运算符有以下6种:

>, <,=, >=, <=, ==, !=,含义与其他很多语言都一样

两个向量比较时将会对每个元素进行比较,例如:

a <- c(1,5,9)
b <- c(2,4,9)

print(a>b)   #输出:[1] FALSE  TRUE FALSE
## [1] FALSE  TRUE FALSE
print(a==b)  #输出:[1] FALSE FALSE  TRUE
## [1] FALSE FALSE  TRUE
print(a!=b)  #输出:[1]  TRUE  TRUE FALSE
## [1]  TRUE  TRUE FALSE

逻辑运算符

R语言的逻辑运算符有:&(逻辑与), |(逻辑或), !(逻辑非),&&(逻辑AND), ||(逻辑OR)

前3个运算符对两个向量的元素逐一进行逻辑运算,后2个只是对两个向量的第一个元素进行运算:

a <- c(0,1,0,FALSE)
b <- c(1,2,TRUE, FALSE)


print(a&b) #输出 [1] FALSE  TRUE FALSE FALSE
## [1] FALSE  TRUE FALSE FALSE
print(a|b) #输出 [1] TRUE  TRUE  TRUE FALSE
## [1]  TRUE  TRUE  TRUE FALSE
print(!a) #输出 [1] TRUE FALSE  TRUE  TRUE
## [1]  TRUE FALSE  TRUE  TRUE
print(a&&b) #输出 [1] FALSE
## Warning in a && b: 'length(x) = 4 > 1' in coercion to 'logical(1)'
## [1] FALSE
print(a||b) #输出 [1] TRUE
## Warning in a || b: 'length(x) = 4 > 1' in coercion to 'logical(1)'
## Warning in a || b: 'length(x) = 4 > 1' in coercion to 'logical(1)'
## [1] TRUE

赋值运算符

  • 左分配符:<-,<<-,=

  • 右分配符:->, ->>

例如:

v1 <- c(1,2,3+1i)
v2 <<- c(1,2,3+1i)
v3 = c(1,2,3+1i)

c(1,2,3+1i) -> v4
c(1,2,3+1i) ->> v5

print(v1)
## [1] 1+0i 2+0i 3+1i
print(v2)
## [1] 1+0i 2+0i 3+1i
print(v3)
## [1] 1+0i 2+0i 3+1i
print(v4)
## [1] 1+0i 2+0i 3+1i
print(v5)
## [1] 1+0i 2+0i 3+1i

其他运算符

  • :(冒号运算符), 将会按顺序创建一系列数字,例如:
t <- 3:8
print(t)   #输出  [1] 3 4 5 6 7 8
## [1] 3 4 5 6 7 8
  • %in%用于判断元素是否属于向量,例如:
a <- 4
b <- 10
c <- 2:6

print(a %in% c)   #输出  [1] TRUE
## [1] TRUE
print(b %in% c)   #输出  [1] FALSE
## [1] FALSE
  • %*% 用于矩阵乘法 (注意:矩阵对应位置元素相乘用*):
M = matrix(c(1,2,3,4,5,6,7,8),nrow = 2, ncol = 4, byrow = TRUE)
print(M)      #输出矩阵   
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
print(t(M))   #将矩阵转置(t()用于转置矩阵)
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6
## [3,]    3    7
## [4,]    4    8
print(M %*% t(M)) #矩阵与其转置矩阵相乘(矩阵乘法)
##      [,1] [,2]
## [1,]   30   70
## [2,]   70  174

3. 工作空间

工作空间(workspace)就是当前R的工作环境,它储存着所有用户定义的对象(向量、矩阵、函数、数据框、列表)。我们在RStudio中的环境管理(environment)窗口中可以直观看到R的工作空间中储存的对象,在历史记录(history)窗口可以查看历史记录。

我们可以在工作空间做很多事情:

直接运行代码和作图

比如

print("hello world")
## [1] "hello world"
plot(density(rnorm(1000,0,1)))

查看函数说明

比如

?plot
## 在以下的程序包里找到了与'plot'题目有关的帮助文件:
## 
##   Package               Library
##   graphics              /Library/Frameworks/R.framework/Versions/4.2/Resources/library
##   base                  /Library/Frameworks/R.framework/Resources/library
## 
## 
## 用第一个相配…

用上边的代码,我们就可以知道plot这个函数的功能、用法等等信息。

查看源代码

比如说,我们想要查看lm这个函数的源代码,只需要直接输入lm,不加括号就可以实现:

lm
## function (formula, data, subset, weights, na.action, method = "qr", 
##     model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
##     contrasts = NULL, offset, ...) 
## {
##     ret.x <- x
##     ret.y <- y
##     cl <- match.call()
##     mf <- match.call(expand.dots = FALSE)
##     m <- match(c("formula", "data", "subset", "weights", "na.action", 
##         "offset"), names(mf), 0L)
##     mf <- mf[c(1L, m)]
##     mf$drop.unused.levels <- TRUE
##     mf[[1L]] <- quote(stats::model.frame)
##     mf <- eval(mf, parent.frame())
##     if (method == "model.frame") 
##         return(mf)
##     else if (method != "qr") 
##         warning(gettextf("method = '%s' is not supported. Using 'qr'", 
##             method), domain = NA)
##     mt <- attr(mf, "terms")
##     y <- model.response(mf, "numeric")
##     w <- as.vector(model.weights(mf))
##     if (!is.null(w) && !is.numeric(w)) 
##         stop("'weights' must be a numeric vector")
##     offset <- model.offset(mf)
##     mlm <- is.matrix(y)
##     ny <- if (mlm) 
##         nrow(y)
##     else length(y)
##     if (!is.null(offset)) {
##         if (!mlm) 
##             offset <- as.vector(offset)
##         if (NROW(offset) != ny) 
##             stop(gettextf("number of offsets is %d, should equal %d (number of observations)", 
##                 NROW(offset), ny), domain = NA)
##     }
##     if (is.empty.model(mt)) {
##         x <- NULL
##         z <- list(coefficients = if (mlm) matrix(NA_real_, 0, 
##             ncol(y)) else numeric(), residuals = y, fitted.values = 0 * 
##             y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w != 
##             0) else ny)
##         if (!is.null(offset)) {
##             z$fitted.values <- offset
##             z$residuals <- y - offset
##         }
##     }
##     else {
##         x <- model.matrix(mt, mf, contrasts)
##         z <- if (is.null(w)) 
##             lm.fit(x, y, offset = offset, singular.ok = singular.ok, 
##                 ...)
##         else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok, 
##             ...)
##     }
##     class(z) <- c(if (mlm) "mlm", "lm")
##     z$na.action <- attr(mf, "na.action")
##     z$offset <- offset
##     z$contrasts <- attr(x, "contrasts")
##     z$xlevels <- .getXlevels(mt, mf)
##     z$call <- cl
##     z$terms <- mt
##     if (model) 
##         z$model <- mf
##     if (ret.x) 
##         z$x <- x
##     if (ret.y) 
##         z$y <- y
##     if (!qr) 
##         z$qr <- NULL
##     z
## }
## <bytecode: 0x7f951efa4b88>
## <environment: namespace:stats>

运行脚本

很多时候,你不想每一次跑程序都重新写,也不想复制粘贴。所以可以写成R脚本(R script)的形式,然后直接运行。R脚本就是普通的文件,只需要再命名文件的时候,结尾用.R来结尾就行了。运行R脚本的方式有很多,不过最常用的大概有两种,一个与交互框有关——通过source()命令运行,另一种是直接再bash命令中调用Rscript运行。

下面假设我们写了一个小的R脚本,命名并保存为TestScript.R。然后在交互框中输入代码source("./TestScript.R"),就可以运行写好的脚本啦,如图:

保存工作空间

在一个R会话结束时,你可以将当前工作空间保存到一个镜像(.Rhistory文件)中,并在下次启动R时自动载入它,例如

savehistory("~/Desktop/try_history.Rhistory")

也可以通过保存的图标来保存

### 工作目录

  • 当前的工作目录(working directory)是R用来读取文件和保存结果的默认目录。我们可以使用函数getwd()来查看当前的工作目录,或使用函数setwd()设定当前的工作目录。

  • 如果需要读入一个不在当前工作目录下的文件,则需在调用语句中写明完整的路径。记得使用引号闭合这些目录名和文件名。

工作空间管理函数

函数 功能
getwd() 显示当前的工作目录
setwd() 修改当前的工作目录为new_path
ls() 列出当前工作空间中的对象
rm(objectList) 移除(删除)一个或多个对象
rm(list=ls()) 移除当前工作空间的所有对象,即清除R工作空间中的内存变量
help(options) 显示可用选项的说明
options() 显示或设置当前选项
history(n) 显示最近使用过的n个命令(默认值为25)
savehistory(“myfile”) 保存命令历史到文件myfile中(默认值为.Rhistory)
loadhistory(“myfile”) 载入一个命令历史文件(默认值为.Rhistory)
save.image(“myfile”) 保存工作空间到文件myfile中(默认值为.RData)
save(objectlist,file=“myfile”) 保存指定对象到一个文件中
load(“myfile”) 读取一个工作空间到当前会话中(默认值为.RData)
q() 退出R,并将会询问是否保存工作空间