5

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

user3262756
  • 649
  • 1
  • 9
  • 27
  • 2
    Apologies, just updated my question. – user3262756 May 29 '17 at 10:01
  • 2
    This is actually a regex question. You should use a regular expression in the `pattern`. However, more importantly, why are these `temp` objects not nicely together in a list? – Roland May 29 '17 at 10:31

4 Answers4

4

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.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
0

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"))
cmbarbu
  • 4,354
  • 25
  • 45
0
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"
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60
  • 1
    Thanks for your code! The problem is that I have dd and temp files together in the environment. So, this solution doesn't really fit. – user3262756 May 29 '17 at 10:53
  • Thanks for clarifying. I created an AND condition. Please run code above and let me know if it worked. Thank you. – Ajay Ohri May 29 '17 at 11:48
0

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())])))
Jan
  • 4,974
  • 3
  • 26
  • 43
ChrisD
  • 50
  • 7