在 R 编程中从内存中删除对象 - rm()函数
R 语言中的rm()
函数用于从内存中删除对象。它可以与ls()
函数一起使用来删除所有对象。 remove()
函数也类似于rm()
函数。
Syntax: rm(x)
Parameters:
x: Object name
示例 1:
# R Program to remove
# objects from Memory
# Creating a vector
vec <- c(1, 2, 3, 4)
vec
# Creating a list
list1 = list("Number" = c(1, 2, 3),
"Characters" = c("a", "b", "c"))
list1
# Creating a matrix
mat <- matrix(c(1:9), 3, 3)
mat
# Calling rm() Function
rm(list1)
# Calling ls() to check object list
ls()
输出:
[1] 1 2 3 4
$Number
[1] 1 2 3
$Characters
[1] "a" "b" "c"
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[1] "mat" "vec"
示例 2:
# R Program to remove
# objects from Memory
# Creating a vector
vec <- c(1, 2, 3, 4)
# Creating a list
list1 = list("Number" = c(1, 2, 3),
"Characters" = c("a", "b", "c"))
# Creating a matrix
mat <- matrix(c(1:9), 3, 3)
# Calling rm() Function
# to remove all objects
rm(list = ls())
# Calling ls() to check object list
ls()
输出:
character(0)