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.

workspace02

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

Advertisements

About norbert

I am post doc at the Department of Medical Psychology and Sociology, Leipzig University (GER), with degrees in sociology (MA) and public health (MPH).
This entry was posted in Data Management. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s