Re: [R] Removing objects and clearing memory

2011-09-27 Thread 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.


--
View this message in context: 
http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3849780.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-13 Thread Jim Silverton
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 ehl...@ucalgary.ca wrote:

 On 2011-04-12 15:52, seeliger.c...@epamail.epa.gov 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]]

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-13 Thread Jim Silverton
On Wed, Apr 13, 2011 at 11:12 AM, Jim Silverton jim.silver...@gmail.comwrote:

 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 ehl...@ucalgary.ca wrote:

 On 2011-04-12 15:52, seeliger.c...@epamail.epa.gov 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]]

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-12 Thread 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

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-12 Thread Rolf Turner

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

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-12 Thread 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]]

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-12 Thread Seeliger . Curt
  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
seeliger.c...@epa.gov
541/754-4638


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-12 Thread Peter Ehlers

On 2011-04-12 15:52, seeliger.c...@epamail.epa.gov 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

__
R-help@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.


Re: [R] Removing objects and clearing memory

2011-04-12 Thread B77S
#replace v with whatever
rm(list=(ls()[ls()!=v]))


--
View this message in context: 
http://r.789695.n4.nabble.com/Removing-objects-and-clearing-memory-tp3445763p3445865.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.