Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove multiple objects with rm()

Tags:

r

My memory is getting clogged by a bunch of intermediate files (call them temp1, temp2, etc.), and I would like to know if it is possible to remove them from memory without doing repeated rm calls (i.e. rm(temp1), rm(temp2))?

I tried rm(list(temp1, temp2, etc.)), but that doesn't seem to work.

like image 204
user702432 Avatar asked Jul 24 '12 05:07

user702432


People also ask

What is RM list ls ()) in R?

The ls() code lists all of the objects in your workspace. The rm() code removes objects in your workspace. You can begin your code with the rm() function to clear all of the objects from your workspace to start with a clean environment.

How do I remove all objects from working memory in R?

rm() function in R Language is used to delete objects from the memory. It can be used with ls() function to delete all objects.

How do I remove all objects in RStudio?

You can do both by restarting your R session in RStudio with the keyboard shortcut Ctrl+Shift+F10 which will totally clear your global environment of both objects and loaded packages.

What is RM function in R?

The rm() function in R is used to delete or remove a variable from a workspace.


3 Answers

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

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

or

rm(temp1, temp2)
like image 195
mnel Avatar answered Oct 19 '22 13:10

mnel


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

like image 42
Alan Avatar answered Oct 19 '22 12:10

Alan


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)
like image 5
Dieter Menne Avatar answered Oct 19 '22 12:10

Dieter Menne