Search⌘ K
AI Features

Basic Methods for Handling Variables

Explore how to manage variables in R by learning to list all variables using the ls() function and delete unwanted variables with the rm() function. This lesson helps you handle your R workspace efficiently by understanding basic commands for variable management.

Listing and Deleting Variables

Listing

We can check all the variables that have been created in the workspace using the keyword ls().

To import recent packages for execution of codes on our platform, we have declared a variable r at our backend. Under normal circumstances, you will not get this variable when using the ls() command.

R
myRealNumeric <- 10
myDecimalNumeric <- 10.0
myCharacter <- "10"
myBoolean <- TRUE
myInteger <- 0:10
myComplex <- 5i
cat("Variables in the current directory: \n")
ls() # returns all the variables created in the workspace alphabetically
cat("\n")

Deleting

We can delete a specific variable from the workspace. The keyword rm() can help us permanently remove one or more objects from the workspace:

R
myRealNumeric <- 10
myDecimalNumeric <- 10.0
myCharacter <- "10"
myBoolean <- TRUE
myInteger <- 0:10
myComplex <- 5i
cat("Variables in the current directory: \n")
ls() # returns all the variables created in the workspace
cat("\n")
cat("Deleting myRealNumeric and myDecimalNumeric \n\n")
rm(myRealNumeric, myDecimalNumeric) # delete the two mentioned variables
cat("Variables in the current directory, now: \n")
ls() # returns all the variables created in the workspace
# myRealNumeric, myDecimalNumeric are now deleted
cat("\n")