Remove multiple objects with rm()

后端 未结 4 979
日久生厌
日久生厌 2020-11-28 03:56

My memory is getting clogged by a bunch of intermediate files (call them temp1, temp2, etc). Is it possible to remove them from memory without doing rm(temp1),

相关标签:
4条回答
  • 2020-11-28 03:58

    Another variation you can try is(expanding @mnel's answer) if you have many temp'x'.

    here "n" could be the number of temp variables present

    rm(list = c(paste("temp",c(1:n),sep="")))
    
    0 讨论(0)
  • 2020-11-28 04:12

    An other solution rm(list=ls(pattern="temp")), remove all objects matching the pattern.

    0 讨论(0)
  • 2020-11-28 04:12

    Or using regular expressions

    "rmlike" <- function(...) {
      names <- sapply(
        match.call(expand.dots = FALSE)$..., as.character)
      names = paste(names,collapse="|")
      Vars <- ls(1)
      r <- Vars[grep(paste("^(",names,").*",sep=""),Vars)]
      rm(list=r,pos=1)
    }
    
    rmlike(temp)
    
    0 讨论(0)
  • 2020-11-28 04:23

    Make the list a character vector (not a vector of names)

    rm(list = c('temp1','temp2'))
    

    or

    rm(temp1, temp2)
    
    0 讨论(0)
提交回复
热议问题