How do I clear only a few specific objects from the workspace?

前端 未结 10 1748
陌清茗
陌清茗 2020-12-04 05:53

I would like to remove some data from the workspace. I know the \"Clear All\" button will remove all data. However, I would like to remove just certain data.

For exa

10条回答
  •  既然无缘
    2020-12-04 06:12

    A useful way to remove a whole set of named-alike objects:

    rm(list = ls()[grep("^tmp", ls())])
    

    thereby removing all objects whose name begins with the string "tmp".

    Edit: Following Gsee's comment, making use of the pattern argument:

    rm(list = ls(pattern = "^tmp"))
    

    Edit: Answering Rafael comment, one way to retain only a subset of objects is to name the data you want to retain with a specific pattern. For example if you wanted to remove all objects whose name do not start with paper you would issue the following command:

    rm(list = grep("^paper", ls(), value = TRUE, invert = TRUE))
    

提交回复
热议问题