Saturday, August 22, 2009

Selectively removing objects in R

Suppose you have created a bunch of objects but want to keep only a, b, and c. You can do the following:

# Create an character vector with the objects to keep:

keepthis<-c("a", "b","c")

# Use the set operator setdiff() to delete only the difference
# between the set objects() and the set keepthis.

rm(list=c(setdiff(objects(), keepthis)))

#You can progressively add objects to keepthis using append():

keepthis<-append(keepthis,c("d", "e"))

No comments:

Post a Comment