Quantcast

Removing objects and clearing memory

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Removing objects and clearing memory

Jim Silverton
How do I remove all objects except one in R?

--
Thanks,
Jim.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Denis Kazakiewicz
On 13.04.2011 00:39, Jim Silverton wrote:
> How do I remove all objects except one in R?
>
>    
rm(list=ls()) #will remove ALL objects

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Rolf Turner-3
On 13/04/11 09:49, Denis Kazakiewicz wrote:
> On 13.04.2011 00:39, Jim Silverton wrote:
>> How do I remove all objects except one in R?
>>
> rm(list=ls()) #will remove ALL objects

But he wanted to remove all objects ***except one***!!!  So what's the
point of this answer?

I can't see any way except a rather round-about kludge to do what the
OP wants.

Create a function:

rmabo <- function(x) {
# Remove all but one.
     fff <- tempfile()
     save(list=deparse(substitute(x)),file=fff)
     rm(list=ls(pos=1),pos=1)
     load(fff,envir=.GlobalEnv)
     unlink(fff)
}

Save this function (otherwise it will get removed when you remove
all objects except one, and rmabo is not the one, and will be lost):

save(rmabo,file=".rmabo.rda")

Whenever you want to remove all objects except one, attach the
data base in which you have saved "rmabo".  (You only need to do
the attaching once per session; if you like you could put the attach
command in your .Rprofile.)

attach(".rmabo.rda")

Then to remove all objects except "melvin" execute rmabo(melvin).
E.g.:

rm(list=ls)
ls()
character(0)
melvin <- clyde <- irving <- fred <- 42
ls()
[1] "clyde"  "fred"   "irving" "melvin"
rmabo(melvin)
ls()
[1] "melvin"

This seems to work.  Anyone have any cleverer ideas to accomplish the
stated objective?

     cheers,

         Rolf Turner

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Denis Kazakiewicz
This should work also

data.frame(ls())
rm(list=ls()[-5]) #for instance


On 13.04.2011 01:45, Rolf Turner wrote:

> On 13/04/11 09:49, Denis Kazakiewicz wrote:
>> On 13.04.2011 00:39, Jim Silverton wrote:
>>> How do I remove all objects except one in R?
>>>
>> rm(list=ls()) #will remove ALL objects
>
> But he wanted to remove all objects ***except one***!!!  So what's the
> point of this answer?
>
> I can't see any way except a rather round-about kludge to do what the
> OP wants.
>
> Create a function:
>
> rmabo <- function(x) {
> # Remove all but one.
>     fff <- tempfile()
>     save(list=deparse(substitute(x)),file=fff)
>     rm(list=ls(pos=1),pos=1)
>     load(fff,envir=.GlobalEnv)
>     unlink(fff)
> }
>
> Save this function (otherwise it will get removed when you remove
> all objects except one, and rmabo is not the one, and will be lost):
>
> save(rmabo,file=".rmabo.rda")
>
> Whenever you want to remove all objects except one, attach the
> data base in which you have saved "rmabo".  (You only need to do
> the attaching once per session; if you like you could put the attach
> command in your .Rprofile.)
>
> attach(".rmabo.rda")
>
> Then to remove all objects except "melvin" execute rmabo(melvin).
> E.g.:
>
> rm(list=ls)
> ls()
> character(0)
> melvin <- clyde <- irving <- fred <- 42
> ls()
> [1] "clyde"  "fred"   "irving" "melvin"
> rmabo(melvin)
> ls()
> [1] "melvin"
>
> This seems to work.  Anyone have any cleverer ideas to accomplish the
> stated objective?
>
>     cheers,
>
>         Rolf Turner


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Curt Seeliger
In reply to this post by Rolf Turner-3
> >> How do I remove all objects except one in R?
> >>
> > rm(list=ls()) #will remove ALL objects
>
> But he wanted to remove all objects ***except one***!!!  So what's the
> point of this answer?
>
> I can't see any way except a rather round-about kludge to do what the
> OP wants. ...

That might work.  Or try this, replacing QQQ with the name of the object
you want to keep.
rm(list=ls()[!grepl('^QQQ$',ls())])

cur
--
Curt Seeliger, Data Ranger
Raytheon Information Services - Contractor to ORD
[hidden email]
541/754-4638


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Peter Ehlers
On 2011-04-12 15:52, [hidden email] wrote:

>>>> How do I remove all objects except one in R?
>>>>
>>> rm(list=ls()) #will remove ALL objects
>>
>> But he wanted to remove all objects ***except one***!!!  So what's the
>> point of this answer?
>>
>> I can't see any way except a rather round-about kludge to do what the
>> OP wants. ...
>
> That might work.  Or try this, replacing QQQ with the name of the object
> you want to keep.
> rm(list=ls()[!grepl('^QQQ$',ls())])
>
> cur

package gdata has function keep() which does just what the OP asks for.

Peter Ehlers

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Brad Schneid
In reply to this post by Jim Silverton
#replace v with whatever
rm(list=(ls()[ls()!="v"]))
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Jim Silverton
In reply to this post by Peter Ehlers
Hey guys thanks very much for al your responses. It was very helpful.

Jim

On Tue, Apr 12, 2011 at 6:59 PM, Peter Ehlers <[hidden email]> wrote:

> On 2011-04-12 15:52, [hidden email] wrote:
>
>> How do I remove all objects except one in R?
>>>>>
>>>>>  rm(list=ls()) #will remove ALL objects
>>>>
>>>
>>> But he wanted to remove all objects ***except one***!!!  So what's the
>>> point of this answer?
>>>
>>> I can't see any way except a rather round-about kludge to do what the
>>> OP wants. ...
>>>
>>
>> That might work.  Or try this, replacing QQQ with the name of the object
>> you want to keep.
>> rm(list=ls()[!grepl('^QQQ$',ls())])
>>
>> cur
>>
>
> package gdata has function keep() which does just what the OP asks for.
>
> Peter Ehlers
>
>


--
Thanks,
Jim.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Jim Silverton
On Wed, Apr 13, 2011 at 11:12 AM, Jim Silverton <[hidden email]>wrote:

> Hey guys thanks very much for al your responses. It was very helpful.
>
> Jim
>
>
> On Tue, Apr 12, 2011 at 6:59 PM, Peter Ehlers <[hidden email]> wrote:
>
>> On 2011-04-12 15:52, [hidden email] wrote:
>>
>>>  How do I remove all objects except one in R?
>>>>>>
>>>>>>  rm(list=ls()) #will remove ALL objects
>>>>>
>>>>
>>>> But he wanted to remove all objects ***except one***!!!  So what's the
>>>> point of this answer?
>>>>
>>>> I can't see any way except a rather round-about kludge to do what the
>>>> OP wants. ...
>>>>
>>>
>>> That might work.  Or try this, replacing QQQ with the name of the object
>>> you want to keep.
>>> rm(list=ls()[!grepl('^QQQ$',ls())])
>>>
>>> cur
>>>
>>
>> package gdata has function keep() which does just what the OP asks for.
>>
>> Peter Ehlers
>>
>>
>
>
> --
> Thanks,
> Jim.
>
>


--
Thanks,
Jim.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Removing objects and clearing memory

Kondor
>>>  How do I remove all objects except one in R?

Here is another way. ls() gives you a list. get a number (n) of the item you'd like to keep,
and then do:

rm(list=ls()[n+1:last])
rm(list=ls()[1:n-1])

ls() should return only one object after this.
Loading...