[R] How to remove all objects except a few specified objects?

Peter Alspach Peter.Alspach at plantandfood.co.nz
Wed Aug 25 04:35:38 CEST 2010


Tena korua

ls()[!(ls() %in% c('keepThis','andThis'))]
will give the elements excluding 'keepThis' and 'andThis'.  Thus

rm(list= ls()[!(ls() %in% c('keepThis','andThis'))])

will remove everything except these two objects, and hidden objects.  It you want to remove the hidden objects as be use ls(all=TRUE).

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Dejian Zhao
> Sent: Wednesday, 25 August 2010 1:32 p.m.
> To: r-help at r-project.org
> Subject: Re: [R] How to remove all objects except a few specified
> objects?
> 
>  If your specified objects have a certain pattern, you can use the
> parameter "pattern" in ls() to remove or keep it.
> rm(list=ls(..., pattern="your_pattern"))
> If not, possibly you have to manually specify them.
> 
> On 2010-8-24 3:00, Cheng Peng wrote:
> > How to remove all R objects in the RAM except for a few specified
> ones?
> > rm(list=ls()) removes all R objects in the R work space.
> >
> > Another question is that whether removing all R objects actually
> releases
> > the RAM? Thanks.
> >
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list