[R] Removing objects from workspace

Ronggui Huang ronggui.huang at gmail.com
Tue Aug 25 06:25:33 CEST 2009

It depends.

If there are patterns in the names, you can make use of pattern
argument of ls(). For example,
> x1="a";x2="b";x3="c"
> ls(pattern="x[1-2]")
[1] "x1" "x2"
> ls(pattern="x[^1-2]")
[1] "x3"
# to remove x1-x3

More generally, if you want to remove all butx,xx,xxx,
you can use
>rm(list=ls()[! ls() %in% c(x,xx,xxx)])

Hope this helps.

2009/8/25 Steven Kang <stochastickang at gmail.com>:
> Hi all,
> I am currently woking with hundreds of objects in workspace and whenever I
> invoke ls() to observe the names of the objects, there are too much of
> unnecessary variables.
> For example, if I only require say 3 or 4 objects from hundreds of objects
> in workspace, are there any methods that may do the job?
> I have tried rm(-c(x,xx,xxx)), but no luck..
> Your feedback in this problem would be highly appreciated.
> Steve
>        [[alternative HTML version deleted]]
> ______________________________________________
> 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.

HUANG Ronggui, Wincent
PhD Candidate
Dept of Public and Social Administration
City University of Hong Kong
Home page: http://asrr.r-forge.r-project.org/rghuang.html

More information about the R-help mailing list