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.