Cheng Peng
2010-Aug-23 19:00 UTC
[R] How to remove all objects except a few specified objects?
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. -- View this message in context: http://r.789695.n4.nabble.com/How-to-remove-all-objects-except-a-few-specified-objects-tp2335651p2335651.html Sent from the R help mailing list archive at Nabble.com.
500600
2010-Aug-24 07:04 UTC
[R] How to remove all objects except a few specified objects?
a <- 1 b <- 2 c <- 3 ls()[-a] # set minus to all the objects you want to retain rm(list = ls()[-a] # will remove all the objects - except a ls() # presto -- View this message in context: http://r.789695.n4.nabble.com/How-to-remove-all-objects-except-a-few-specified-objects-tp2335651p2336200.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2010-Aug-24 14:27 UTC
[R] How to remove all objects except a few specified objects?
If you are going to be doing this a lot, you may want to consider
making a little function. Here is an example 'do not remove'
function. It defaults to the global environment, but you can always
change it. Also, if you try to keep a variable name that does not
exist, it will throw an error and not remove anything. My assumption
is that when one of the names the user typed does not match a current
object, the user made a typo.
dnrm <- function(vars, envir = .GlobalEnv) {
vars <- c(vars, "dnrm")
keep <- match(x = vars, table = ls(envir = envir))
if(any(is.na(keep))) {
stop(paste("Some of the variables were not found in",
environmentName(envir)))
}
rm(list = ls(envir = envir)[-keep], envir = envir)
cat("Removed all but", length(keep), "objects from",
environmentName(envir), fill = TRUE)
}
Cheers,
Josh
On Mon, Aug 23, 2010 at 12:00 PM, Cheng Peng <cpeng at usm.maine.edu>
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.
> --
> View this message in context:
http://r.789695.n4.nabble.com/How-to-remove-all-objects-except-a-few-specified-objects-tp2335651p2335651.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
Karl Brand
2010-Aug-24 15:13 UTC
[R] How to remove all objects except a few specified objects?
Hi Cheng, Check out the keep() function in package:gdata. And to be sure the "removed" objects are really removed from system memory i think you need to run gc(). hth, Karl On 8/23/2010 9:00 PM, 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.-- Karl Brand <k.brand at erasmusmc.nl> Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam P +31 (0)10 704 3409 | F +31 (0)10 704 4743 | M +31 (0)642 777 268
Cheng Peng
2010-Aug-24 21:42 UTC
[R] How to remove all objects except a few specified objects?
Thanks Josh and Karl. function dnrm() works well for my purpose. -- View this message in context: http://r.789695.n4.nabble.com/How-to-remove-all-objects-except-a-few-specified-objects-tp2335651p2337398.html Sent from the R help mailing list archive at Nabble.com.
Dejian Zhao
2010-Aug-25 01:32 UTC
[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. >