當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 rm()用法及代碼示例


rm()R語言中的函數用於從內存中刪除對象。它可以與ls()函數刪除所有對象。remove()函數也類似於rm()函數。

用法: rm(x)

參數:
x:對象名稱

範例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)



相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Remove Objects from Memory in R Programming – rm() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。