r – What is the difference between rm() and rm(list=ls())?

r – What is the difference between rm() and rm(list=ls())?

Command rm(list=ls()) removes all objects from the current workspace (R memory), whereas rm() alone does not do anything. You have to specify to rm() what you want to remove. For example,

a<-1
rm(a)

would remove object a from you workspace. In contrast,

a<-1
b<-2
rm(a)

would remove just the object a from the memory, but leave the object b untouched. The following would remove both a and b:

a<-1
b<-2
rm(list=ls())

rm(list=ls()) is easier to write than rm(a, b), which also removes a and b from your environment, and scales to any number of objects. Imagine removing 100 objects by name: rm(a,b,c,d,e,f,g,h) and so on…

You can give rm() a bunch of object to remove using the argument list. Because ls()lists all objects in the current workspace, and you specify it as the list of objects to remove, the aforementioned command removes all objects from the R memory.

rm() is basically remove{base}, it is used to Remove Objects from a Specified Environment.

Command rm(list=ls()) means-

list=ls() is base in this command that means you are referring to all the objects present in the workspace.

similarly, rm() is used to remove all the objects from the workspace when you use list=ls() as base.

However,
when it comes to using rm() alone it will not do anything as the base is absent.

You can use rm() to remove specific variables to by putting them as base:

For example

a <-45                                                                          
rm(a)

r – What is the difference between rm() and rm(list=ls())?

Leave a Reply

Your email address will not be published.