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 (TRUEorFALSE) 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 variablesa <- 4b <- sqrt(a)c <- b * bd <- c * b# listing all the variables in the present working directoryls()# removing variablesrm(list = c("a", "d"))ls()
Explanation
- Lines 4–7: We create variables
a,b,c, andd. - 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 variablesaandd. - Line 15: Using the
ls()function once again, we return the list of all the variables present in the workspace.