What is the rm() function in R?

Overview

The rm() function in R is used to delete or remove a variable from a workspace.

Syntax

The rm() function takes the following syntax:

rm (…, list = character(), envir = as.environment(pos), inherits = FALSE)
Syntax for the rm() function in R

Parameters

The rm() function takes the following parameter values:

  • ...: This represents the objects to be removed.
  • list: This represents a character vector that names the variables to be removed.
  • envir: This indicates the environment to be used.
  • inherits: This takes a boolean value (TRUE or FALSE) and determines if the closing frame of the environment should be inspected or not.

Example

# A code to illustrate the rm() function in R
# creating variables
a <- 4
b <- sqrt(a)
c <- b * b
d <- c * b
# listing all the variables in the present working directory
ls()
# removing variables
rm(list = c("a", "d"))
ls()

Explanation

  • Lines 4–7: We create variables a, b, c, and d.
  • Line 10: We call the ls() function to return a list of all the variables in the workspace.
  • Line 13: Using the rm() function, we remove variables a and d.
  • Line 15: Using the ls() function once again, we return the list of all the variables present in the workspace.

Free Resources