R – 在内存中创建、列出和删除对象
关于 R 最有趣的事实之一是,在 R 中被称为对象的东西在许多其他编程语言中被称为变量。根据上下文,对象和变量可能具有截然不同的含义。在每一种计算机语言中,变量都提供了一种访问存储在内存中的数据的方法。 R 不提供对计算机内存的直接访问,而是提供了几个专门的数据结构,我们将其称为对象。这些对象通过符号或变量来引用。然而,在 R 中,符号本身就是对象,可以像任何其他对象一样进行操作。这与许多其他语言不同,具有广泛的影响。 R中的所有实体都可以称为对象。它们可以是数组、数字、字符串、列表、向量、数据框等。
在内存中创建对象
要在 R 中做有用和有趣的事情,需要为对象赋值。要在 R 中创建一个对象,需要给它一个名称,后跟赋值运算符<- (也可以使用等号= ),以及他想要给它的值:
x <- 5
<-是赋值运算符。它将右侧的值分配给左侧的对象。因此,在执行x <- 5之后,x 的值为 5。箭头可以读作 5 进入 x。
对象命名规则:
- 可以为对象指定任何名称,例如 x、personName 或 subject_id。
- 使对象名称明确且不要太长。
- 对象名称不能以数字开头(2x 无效,但 x2 有效)。
- R 区分大小写(例如,subject_id 与 Subject_id 不同)。
- 有一些名称不能使用,因为它们是 R 中基本函数的名称(例如,if、else、for,完整列表请参见 R 中的保留字)。
- 一般来说,即使允许,最好不要使用其他函数名(例如,c、T、mean、data、df、weights等)。
- 最好在 my.dataframe 中的变量名中避免使用点 (.)。由于历史原因,R 中有许多函数名称中带有点,但由于点在 R(用于方法)和其他编程语言中具有特殊含义,因此最好避免使用它们。
- 还建议变量名使用名词,函数名使用动词。
- 保持代码样式的一致性很重要(他在哪里放置空格,他如何命名变量等)。使用一致的编码风格可以让一个人的代码更清晰,便于他未来的自己和他的合作者阅读。
例子:
# R program to illustrate
# Creating an object in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
输出:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
在内存中列出对象
可以使用objects()
或ls()
函数列出其工作目录中的所有对象。 objects()
或ls()
函数可用于获取环境中所有对象名称的字符字符串。
例子:
# R program to illustrate
# Listing objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# Listing objects using object()
cat("Using object()\n")
print(objects())
# Listing objects using ls()
cat("Using ls()\n")
print(ls())
输出:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
Using object()
[1] "listOfNumber" "myDataFrame" "name" "vec1" "vec2"
[6] "x"
Using ls()
[1] "listOfNumber" "myDataFrame" "name" "vec1" "vec2"
[6] "x"
可能会注意到objects()
或ls()
函数以排序顺序返回结果。
列出满足特定模式的对象:
在 R 中,还可以列出指定特定正则表达式的对象。
例子:
# R program to illustrate
# Listing objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# The code below returns all objects
# whose name contains an v
print(ls(pattern = "v"))
# The code below returns all objects
# whose name ends with "e"
print(ls(pattern = "e$"))
输出:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
[1] "vec1" "vec2"
[1] "myDataFrame" "name"
删除内存中的对象
可以使用rm()
或remove()
函数删除其工作目录中的对象。 rm()
或remove()
函数可用于释放内存并清除环境空间。
例子:
# R program to illustrate
# Deleting objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# Deleting object x using rm()
rm(x)
# Deleting object myDataFrame using remove()
remove(myDataFrame)
cat("After deleted following objects listing of the object:\n")
print(ls())
输出:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
After deleted following objects listing of the object:
[1] "listOfNumber" "name" "vec1" "vec2"
删除内存中的所有对象
也可以通过将参数list = ls()
传递给函数rm()
或remove()
来删除内存中的所有对象。
例子:
# R program to illustrate
# Deleting objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# Deleting all objects using rm()
rm(list = ls())
cat("After deleted all objects listing of the object:\n")
print(ls())
输出:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
After deleted all objects listing of the object:
character(0)