[R] Selectively Removing objects

Ben Bolker bolker at ufl.edu
Mon Feb 2 14:41:18 CET 2009


Paulo Grahl <pgrahl <at> gmail.com> writes:

> 
> Dear list members,
> 
> Does anyone know how to use rm() to remove only variables but not
> declared functions from the environment ?
> I understand I could name all the functions with, let's say
> "f_something", make sure that all variables do not start with "f_" and
> then remove all BUT objects starting with "f_".
> However, I have already defined all the functions and it would be
> troublesome to change all of them to a new name.
> 

> a <- 1
> b <- 2
> d <- function(x) { x^2 }
> objs <- ls()
> objclasses <- sapply(objs,function(x) class(get(x)))
> objclasses
         a          b          d 
 "numeric"  "numeric" "function" 
> rm(list=objs[objclasses!="function"])
> ls()
[1] "d"          "objclasses" "objs"      
> 

  (objclasses and objs are left over, but
a and b have been deleted)




More information about the R-help mailing list