top of page
Writer's pictureEkta Aggarwal

Removing different objects form R environment

In your R studio, while you are working, in your top right side- you can see the global environment where information about multiple datasets, variables and functions is stored.

Following is the snippet of my global environment:


What if you want to delete all of the datasets in one go (or want to keep only a few of the datasets)?


Luckily rm( ) function comes to our rescue in such a situation.


Case 1: Removing all of the variables from the environment


rm(list = ls())

ls( ) is a character vector which contains the list of all the objects in the environment. In rm( ) we pass list = ls( ) defining that delete the list of all of these variables.


Case 2: Removing only some of the objects.


Suppose if I want to delete only three objects (data1, data2 and data 3), I shall write the following:

rm(data1,data2,data3)


Case 3: Removing all of the objects from the environment, except a few.


rm(list = ls()[!ls() %in% c("data2","data4")])

In the above command !ls( ) %in% c("data2","data4") returns a logical vector where it will take values as FALSE when dataset name is either data2 or data4. This will tell R that delete all the datasets except data2 and data4.

Comments


bottom of page