[R] Selectively Removing objects

Gustaf Rydevik gustaf.rydevik at gmail.com
Mon Feb 2 14:46:45 CET 2009


On Mon, Feb 2, 2009 at 2:16 PM, Paulo Grahl <pgrahl at gmail.com> wrote:
> 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.
>
> Any hint ?
> Thanks
>
> Paulo Gustavo Grahl, CFA
>


[Note to Paulo:I changed the code slightly: defining Nonfunctions
separately messed things up.]


Hi Paulo,

The following should do it.

test<-function(x)x^2
test2<-5
test3<-77
ls()

rm(list=ls()[
sapply(ls(),
       function(x){
         class(get(x))!="function"
         })
])
ls()

Regards,

Gustaf

-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik




More information about the R-help mailing list