How can I remove all objects but one from the workspace in R?

R

R Problem Overview


I have a workspace with lots of objects and I would like to remove all but one. Ideally I would like to avoid having to type rm(obj.1, obj.2... obj.n). Is it possible to indicate remove all objects but these ones?

R Solutions


Solution 1 - R

Here is a simple construct that will do it, by using setdiff:

rm(list=setdiff(ls(), "x"))

And a full example. Run this at your own risk - it will remove all variables except x:

x <- 1
y <- 2
z <- 3
ls()
[1] "x" "y" "z"

rm(list=setdiff(ls(), "x"))

ls()
[1] "x"

Solution 2 - R

Using the keep function from the gdata package is quite convenient.

> ls()
[1] "a" "b" "c"

library(gdata)
> keep(a) #shows you which variables will be removed
[1] "b" "c"
> keep(a, sure = TRUE) # setting sure to TRUE removes variables b and c
> ls()
[1] "a"

Solution 3 - R

I think another option is to open workspace in RStudio and then change list to grid at the top right of the environment(image below). Then tick the objects you want to clear and finally click on clear.

enter image description here

Solution 4 - R

I just spent several hours hunting for the answer to a similar but slightly different question - I needed to be able to delete all objects in R (including functions) except a handful of vectors.

One way to do this:

rm(list=ls()[! ls() %in% c("a","c")])

Where the vectors that I want to keep are named 'a' and 'c'.

Hope this helps anyone searching for the same solution!

Solution 5 - R

To keep all objects whose names match a pattern, you could use grep, like so:

to.remove <- ls()
to.remove <- c(to.remove[!grepl("^obj", to.remove)], "to.remove")
rm(list=to.remove)

Solution 6 - R

Replace v with the name of the object you want to keep

rm(list=(ls()[ls()!="v"]))

hat-tip: http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html

Solution 7 - R

To keep a list of objects, one can use:

rm(list=setdiff(ls(), c("df1", "df2")))

Solution 8 - R

This takes advantage of ls()'s pattern option, in the case you have a lot of objects with the same pattern that you don't want to keep:

> foo1 <- "junk"; foo2 <- "rubbish"; foo3 <- "trash"; x <- "gold"  
> ls()
[1] "foo1" "foo2" "foo3" "x"   
> # Let's check first what we want to remove
> ls(pattern = "foo")
[1] "foo1" "foo2" "foo3"
> rm(list = ls(pattern = "foo"))
> ls()
[1] "x"

Solution 9 - R

require(gdata)
keep(object_1,...,object_n,sure=TRUE)
ls()

Solution 10 - R

From within a function, rm all objects in .GlobalEnv except the function

initialize <- function(country.name) {
  
  if (length(setdiff(ls(pos = .GlobalEnv), "initialize")) > 0) {
    rm(list=setdiff(ls(pos = .GlobalEnv), "initialize"), pos = .GlobalEnv)
  }

}

Solution 11 - R

let's think in different way, what if we wanna remove a group? try this,

 rm(list=ls()[grep("xxx",ls())]) 

I personally don't like too many tables, variables on my screen, yet I can't avoid using them. So I name the temporary things starting with "xxx", so I can remove them after it is no longer used.

Solution 12 - R

# remove all objects but selected
rm(list = ls()[which("key_function" != ls())])

Solution 13 - R

How about this?

# Removes all objects except the specified & the function itself.

rme <- function(except=NULL){
  except = ifelse(is.character(except), except, deparse(substitute(except)))
  rm(list=setdiff(ls(envir=.GlobalEnv), c(except,"rme")), envir=.GlobalEnv)
}

Solution 14 - R

assuming you want to remove every object except df from environment:

rm(list = ls(pattern="[^df]"))

Solution 15 - R

The following will remove all the objects from your console

rm(list = ls())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDQdlMView Question on Stackoverflow
Solution 1 - RAndrieView Answer on Stackoverflow
Solution 2 - RRahul PremrajView Answer on Stackoverflow
Solution 3 - RSadeghView Answer on Stackoverflow
Solution 4 - RTalonView Answer on Stackoverflow
Solution 5 - RAaron left Stack OverflowView Answer on Stackoverflow
Solution 6 - RBenView Answer on Stackoverflow
Solution 7 - RPeterView Answer on Stackoverflow
Solution 8 - RPeter DiakumisView Answer on Stackoverflow
Solution 9 - RHollyView Answer on Stackoverflow
Solution 10 - RGriffith FeeneyView Answer on Stackoverflow
Solution 11 - RGrec001View Answer on Stackoverflow
Solution 12 - Rf0nzieView Answer on Stackoverflow
Solution 13 - RthinkView Answer on Stackoverflow
Solution 14 - RMCHView Answer on Stackoverflow
Solution 15 - RVishnu KumarView Answer on Stackoverflow