Re: [R] wildcards and removing variables

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Tue 11 Oct 2005 - 01:08:47 EST

On Mon, 2005-10-10 at 16:01 +0100, Prof Brian Ripley wrote:
> On Mon, 10 Oct 2005, Marc Schwartz (via MN) wrote:
>
> > On Mon, 2005-10-10 at 10:37 -0400, Afshartous, David wrote:
> >> All,
> >>
> >> Is there are a wildcard in R for varible names as in unix? For example,
> >>
> >> rm(results*)
> >>
> >> to remove all variable or function names that begin w/ "results"?
> >>
> >> cheers,
> >> Dave
> >> ps - please respond directly to afshar@miami.edu
> >
> >
> > See ?ls, which has a 'pattern' argument, enabling the use of Regex to
> > define the objects to be listed and subsequently removed using rm().
> >
> > You can then use something like:
> >
> > rm(list = ls(pattern = "\\bresults."))
>
> One new feature of R-2.2.0 is glob2rx, which converts wildcards to regexps
> for you. E.g.
>
> rm(list = ls(pattern = glob2rc("results*")))
>
> (I think if does it a little better, as that trailing dot is not I think
> correct: result* matches result.)

Thanks to both Gabor and Prof. Ripley for pointing out the use of glob2rc(). One of the new features I had forgotten about since reading NEWS. On the trailing dot, I was just in the process of drafting a follow up after realizing my error, since as you point out, 'results' would not be matched in that case.

Thanks,

Marc



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Oct 11 01:21:30 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:37:38 EST