Using the rm() Function in R
Using the rm() Function in R Syntax rm(list = c(“object1”, “object2”), envir = .GlobalEnv) list: A character vector of the names of objects to remove. envir: The environment from which to remove objects. By default, it is the global environment (.GlobalEnv). Basic Usage To remove a single object: # Create an object x <- 42 # Remove the object rm(x) # Verify that the object has been removed exists(“x”) # Returns FALSE To remove multiple objects: # Create multiple objects a <- 10 b <- “text” c <- 3.14 # Remove multiple objects rm(a, b, c) # Check if objects are removed exists(“a”) # Returns FALSE exists(“b”) # Returns FALSE exists(“c”) # Returns FALSE Using the list Argument You can specify objects to remove by passing a character vector of their names to the list argument. # Create objects var1 <- 1 var2 <- 2 var3 <- 3 # Remove objects using a list rm(list = c(“var1”, “var3”)) # Check remaining objects exists(“var1”) # Returns FALSE exists(“var2”) # Returns TRUE exists(“var3”) # Returns FALSE Removing All Objects To remove all objects from the workspace, use rm() with list = ls(): # Create some objects foo <- 1 bar <- 2 # Remove all objects rm(list = ls()) # Verify that all objects are removed ls() # Returns character(0) indicating no objects left Removing Objects from a Specific Environment You can remove objects from a specific environment (not necessarily the global environment) by specifying the envir argument. # Create a specific environment my_env <- new.env() my_env$var1 <- 100 my_env$var2 <- 200 # Remove an object from the specific environment rm(list = “var1”, envir = my_env) # Check remaining objects in the environment ls(envir = my_env) # Returns “var2” Practical Examples Removing a Single Object # Create an object temp_data <- c(1, 2, 3) # Remove the object rm(temp_data) # Verify removal exists(“temp_data”) # Returns FALSE Removing Multiple Objects # Create objects x <- 1 y <- 2 z <- 3 # Remove selected objects rm(x, y) # Check remaining objects exists(“x”) # Returns FALSE exists(“y”) # Returns FALSE exists(“z”) # Returns TRUE Removing All Objects # Create objects a <- 10 b <- 20 # Remove all objects rm(list = ls()) # Check if all objects are removed ls() # Returns character(0) Removing Objects from a Specific Environment # Create a new environment and add objects my_env <- new.env() my_env$obj1 <- “foo” my_env$obj2 <- “bar” # Remove an object from this environment rm(list = “obj1”, envir = my_env) # Check remaining objects in the environment ls(envir = my_env) # Returns “obj2” Summary The rm() function in R is a powerful tool for managing your workspace by removing objects that are no longer needed. You can remove one or more objects, specify objects using a character vector, and even clear all objects from the workspace. Additionally, you can target specific environments to remove objects, providing flexibility in managing different contexts within your R sessions.
Using the rm() Function in R Lire la suite »