Removing workspace objects whose name start by a pattern using R

前端 未结 1 462
谎友^
谎友^ 2021-01-11 23:48

I often create temporary objects whose names start by \'tp_\' and use user-defined function. In order to keep a clean workspace, I would like to create a function that remov

相关标签:
1条回答
  • 2021-01-12 00:06

    The pattern argument uses regular expressions. You can use a caret ^ to match the beginning of the string:

    rm(list=ls(pattern="^tp_"))
    rm(list=setdiff(ls(pattern = "^tp_"), lsf.str()))
    

    However, there are other patterns for managing temporary items / keeping clean workspaces than name prefixes.

    Consider, for example,

    temp<-new.env()
    temp$x <- 1
    temp$y <- 2
    with(temp,x+y)
    #> 3
    rm(temp)
    

    Another possibility is attach(NULL,name="temp") with assign.

    0 讨论(0)
提交回复
热议问题