How to remove objects from workspace

Since every object in the R workspace requires some part of the RAM, it is sometimes useful to remove objects from workspace being not necessary for further calculations. In R, there are several ways to ged rid of such objects.

The above image shows the workspace pane of RStudio.

If you intend to remove all objects from workspace, use the following code:

rm(list = ls())

If you want to remove some objects (in this case we remove the objects a, b and c) from workspace, use the following code. Each object you want to remove must be specified between the brackets separated by comma.

rm(a, b, c)

Sometimes it may occurr that you want to remove more than just a few but not all objects. In this case, specifying every single object would require a lot of typing.

In case you want to remove all objects but a few (e.g. the dataframe(s) you work with), you can use the following code.

rm(list = c(setdiff(ls(), c("mydata.1", "mydata.2"))))

The above code will remove all objects except for the objects named mydata.1 and mydata.2.


The above image shows the workspace containing only the desired objects.

Author: norbert

Biometrician at Clinical Trial Centre, Leipzig University (GER), with degrees in sociology (MA) and public health (MPH).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: