[R] extracting variables with patttern templates

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue Oct 22 22:43:05 CEST 2002


What is X?  The only things I can think of in R that could be thought to
contain datasets are lists, and they are extracted via [[ ]].
Or perhaps X is a data frame and you meant columns (`variables' but
perhaps also more general objects).

In either case you can do this by indexing.  To extract all the columns of
a data frame whose name contains `a' I can use

X[grep("a", names(X))]

and that allows regular expressions just like the `pattern' argument of
ls/objects.

It's hard to give precise answers to vague questions, but I hope that
gives
you enough of an idea.

On Tue, 22 Oct 2002, Rishabh Gupta wrote:

> Hi all,
>  I was wondering, does R have a facility to extract datasets based on a string template. I know that you can do something like:
>     X["a"]    # extract from 'X' the dataset named 'a'.
>
> What I would like is something which says, from 'X' extract all the datasets that have an 'a':
>     X[pat = "a"]
>
> I know that the 'pat' option can be used when just listing all the object that exist, but is there any way to extract only certain
> dataset from within an object using a smiliar technique.
> Any help would be greatly appreciated.
>
> Many Thanks!
>
> Rishabh
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list