Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rm()删除多个对象_R - Fatal编程技术网

使用rm()删除多个对象

使用rm()删除多个对象,r,R,我的内存被一堆中间文件阻塞了(称它们为temp1,temp2,等等)。我想知道是否可以在不重复执行rm调用的情况下从内存中删除它们(即rm(temp1),rm(temp2)) 我尝试了rm(列表(temp1、temp2等),但似乎不起作用。将列表设置为字符向量(而不是名称向量) 或 或者使用正则表达式 "rmlike" <- function(...) { names <- sapply( match.call(expand.dots = FALSE)$..., as.c

我的内存被一堆中间文件阻塞了(称它们为
temp1
temp2
,等等)。我想知道是否可以在不重复执行
rm
调用的情况下从内存中删除它们(即
rm(temp1)
rm(temp2)


我尝试了
rm(列表(temp1、temp2等)
,但似乎不起作用。

将列表设置为字符向量(而不是名称向量)


或者使用正则表达式

"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)

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

您可以尝试的另一种变体是(扩展@mnel的答案) 如果你有很多临时'x'

此处“n”可能是存在的临时变量数

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

要删除内存中的所有内容,如果要确保获得所有内容,可以说:rm(list=ls())@Sam
rm(list=ls(all=TRUE))
。它在
%>%
中工作吗?例如
list(…)%%>%rm(list=)
请您解释一下
list
的优点是什么?在我看来,在第二个选项中键入所有变量名而不是只键入TAB autocomplete是不必要的复杂。Josh Paulson描述了这一点(我不知道
ls(…)
做了什么,但现在我猜这就像Unix bash函数ls?)--哇,Josh Paulson使用@Sam
描述的特定种类来删除内存中的所有内容,您可以说:rm(list=ls())
这工作正常,但可能有一个小错误。如果有一个名为“ABCtemp”的对象,它也将被删除。如何删除以“temp”开头的对象并保留“ABCtemp”?您只需向模式中添加更多标准即可。例如,
pattern=“^temp”
将只捕获以“temp”开头的变量,因此不会捕获变量
ABCtemp
。另一种可能性是@BrodieG在此处给出的答案
"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)
rm(list = c(paste("temp",c(1:n),sep="")))