简体   繁体   English

使用 rm() 删除多个对象

[英]Remove multiple objects with rm()

My memory is getting clogged by a bunch of intermediate files (call them temp1 , temp2 , etc.).我的记忆被一堆中间文件(称它们为temp1temp2等)堵塞了。 and I would like to know if it is possible to remove them from memory without doing repeated rm calls (ie rm(temp1) , rm(temp2) )?我想知道是否有可能从内存中没有做重复删除它们rm调用(即rm(temp1)rm(temp2)

I tried rm(list(temp1, temp2, etc.)) , but that doesn't seem to work.我试过rm(list(temp1, temp2, etc.)) ,但这似乎不起作用。

Make the list a character vector (not a vector of names)使列表成为字符向量(不是名称向量)

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

or或者

rm(temp1, temp2)

另一个解决方案rm(list=ls(pattern="temp")) ,删除与模式匹配的所有对象。

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)

Another variation you can try is(expanding @mnel's answer) if you have many temp'x'.如果您有很多 temp'x',您可以尝试的另一种变体是(扩展@mnel 的答案)。

here "n" could be the number of temp variables present这里的“n”可能是存在的临时变量的数量

rm(list = c(paste("temp",c(1:n),sep="")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM