user3262756
user3262756

Reputation: 679

Remove everything from R environment except a specific item and others following pattern

I would like to clean my R environment except for a data frame called "dd" and other data frames that start with "temp" (pattern). I've tried with different modifications of the code below but I cannot make it work. Any idea very much appreciated!

To remove everything except "dd":

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

To remove everything containing "temp":

rm(list = ls(pattern = "temp"))

I want to keep in the environment "dd" and anything that starts with "temp".

Upvotes: 5

Views: 3138

Answers (4)

ChrisD
ChrisD

Reputation: 60

The answer provided by SymbolixAU user:5977215 on this page rm( ) everything except specific object can be adapted e.g.:

dd <- 1
temp1 <- 1
temp2 <- 2
temp3 <- 3
bb1 <- 1
bb2 <- 2
bb3 <- 3

rm(list = setdiff(ls(), c("dd", ls()[grep("temp", ls())])))

Upvotes: 0

Ajay Ohri
Ajay Ohri

Reputation: 3492

PART 1
a=23
dd=45
c=36
d=67
x=ls()
x[x != "dd"];
a" "c" "d"

rm(list=x[x != "dd"])
ls()
[1] "dd" "x"
rm(x)
ls()
[1] "dd"

PART2
Let temp be the pattern in name of object

temp =23
b=45
c=36
dd=67
temp1=20
temp2=40
temp3=50
x=ls()
grepl("temp",x)
[1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE

x[x = grepl("temp",x)]
[1] "temp"  "temp1" "temp2" "temp3"

 x[x != grepl("temp",x)]
[1] "temp"  "temp1" "temp2" "temp3" "b"  "c"  "d" 

 x
[1] "temp"  "temp1" "temp2" "temp3" "b"  "c"  "d" 

x[x!=x[x = grepl("temp",x)]]
[1] "b" "c" "d"


rm(list=x[x!=x[x = grepl("temp",x)]])
Warning message:
In x != x[x = grepl("temp", x)] :
  longer object length is not a multiple of shorter object length
ls()
[1] "temp"  "temp1" "temp2" "temp3" "x" 
rm(x)
ls()
[1] "temp"  "temp1" "temp2" "temp3"


PART 3 Combining them all

temp =23
b=45
c=36
dd=67
temp1=20
temp2=40
temp3=50
x=ls()

#USING AND CONDITION


rm(list=x[x != "dd" & x!=x[x = grepl("temp",x)]])

Warning message:
In x != x[x = grepl("temp", x)] :
  longer object length is not a multiple of shorter object length
> ls()
[1] "dd"    "temp"  "temp1" "temp2" "temp3"

Upvotes: 0

Joris Meys
Joris Meys

Reputation: 108533

Using regular expressions is indeed key here. Let's assign a couple of variables:

obj <- c("dd", "temp1","temmie", "atemp")
for(i in obj) assign(i, rnorm(10))

gives:

> ls()
[1] "atemp"  "dd"     "i"      "obj"    "temmie" "temp1" 

Now it's a 2-step process: First construct a regular expression that:

  1. checks whether something starts with "temp" or is exactly "dd".
  2. inverts the selection, so it returns everything that doesn't match
  3. returns the value instead of the index

This is done with following code:

toremove <- grep("^temp|^dd$", ls(), 
                 invert = TRUE, 
                 value = TRUE)

Now you can simply:

> rm(list = c(toremove, "toremove"))
> ls()
[1] "dd"    "temp1"

You shouldn't forget to remove the list of objects as well, as that one is generated after the call to ls() in grep.

Upvotes: 4

cmbarbu
cmbarbu

Reputation: 4534

Splitting lines would make it easier to code and to read:

 allInMem <- ls()
 toRemove <- allInMem[grep("a|c",allInMem,invert=TRUE)]
 rm(list=c(toRemove,"allInMem","toRemove"))

Upvotes: 0

Related Questions