#==============================================================================
# Remove objects from the workspace (Beginner → Intermediate)
#==============================================================================
#==============================================================================
# List objects currently in the global environment (like SAS PROC CONTENTS for WORK)
#==============================================================================
ls() # or objects()
#==============================================================================
# Remove a single object by name
#==============================================================================
x <- 1
rm(x) # 'x' is now removed
"x" %in% ls() # FALSE
#==============================================================================
# Remove multiple objects
#==============================================================================
a <- 10; b <- 20; c <- 30
rm(a, b)
ls()
#==============================================================================
# Remove objects by character vector
#==============================================================================
to_drop <- c("b", "c")
rm(list = to_drop)
#==============================================================================
# Keep only selected objects (drop everything else)
#==============================================================================
keep <- c("adsl", "adlb") # example object names we want to keep
rm(list = setdiff(ls(), keep))
#==============================================================================
# Remove objects matching a name pattern
#==============================================================================
tmp_01 <- 1; tmp_02 <- 2; tmp_final <- 3
rm(list = ls(pattern = "^tmp_\d+$")) # removes tmp_01 and tmp_02, keeps tmp_final
#==============================================================================
# Clear the entire environment (use with care)
#==============================================================================
# Equivalent to starting a fresh WORK library in SAS mid-session
rm(list = ls())
#==============================================================================
# Tips
#==============================================================================
# 1) Use ls() to preview what will be removed before calling rm()
# 2) Prefer explicit lists (rm(list = ...)) for reproducible scripts
# 3) In RStudio, you can also click the 'broom' in Environment pane to clear all