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

前端 未结 2 1155
灰色年华
灰色年华 2020-12-30 06:16

Most of articles, I have read. They recommend to use rm(list=ls()) but I do not know what is the difference if I like to use rm()

Can I use

相关标签:
2条回答
  • 2020-12-30 06:54

    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.

    0 讨论(0)
  • 2020-12-30 07:01

    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)
    
    0 讨论(0)
提交回复
热议问题