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

前端 未结 2 1178
灰色年华
灰色年华 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.

提交回复
热议问题