How to Remove Variables from your Workspace in R

03.21.2021

To remove variables from our R workspace, we can use the built-in rm function. This method as a few options, so let's take a look at a few of them.

The first thing we can do, is remove one variable at a time. We simply pass the variable to the rm function and it will be deleted.

x = 34
rm(x)
print(x) # x is gone

We can also pass multiple valures to the rm function and it will remove each of them.

x = 34
my.string = "Hello"
rm(x, my.string)

Finally, we use the ls method in a previous article. This ls function return a list of all variables in our workspace. If we want to clear our workspace, we can pass the list from ls to rm.

rm(list = ls())