[R] Regular expressions
Ales Ziberna
aleszib2 at gmail.com
Wed Jan 11 22:02:12 CET 2006
Thank you!
This is definitely an improvement!
Best,
Ales Ziberna
-----Original Message-----
From: pd at pubhealth.ku.dk [mailto:pd at pubhealth.ku.dk] On Behalf Of Peter
Dalgaard
Sent: Wednesday, January 11, 2006 7:24 PM
To: Ales Ziberna
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Regular expressions
"Ales Ziberna" <aleszib at gmail.com> writes:
> Matching regular expressions
>
> Dear useRs!
>
> I have the following problem. I would like to find objects in my
> environment that have two strings in it. For example, I might want to
> find objects that have in their names "MY" and "TARGET". I do not care
> about the ordering of these two substrings in the name, neither what
> is in front, behind or between them, the only thing important is that
> both words are present. I apologize if this is covered in help pages
> (then I did not understand it by reading them several times) or it was
> answered previously (then I did not find it).
>
> Since "ls" with argument pattern essentially uses "grep" (if I am not
> mistaken), I have an example for "grep"
>
> text<-c("somethigMYsomthing elseTARGET another thing","MY somthing
> TARGET another thing","somethig somthing elseTARGETMY another
> thing","somethigMTARGETY another thing")
>
> grep(pattern="MY&TARGET", x=text)
> #I would like to get 1 2 3 and not 4 or actually their names using
> text[grep(pattern="MY&TARGET", x=text)] #of course, the "pattern" in
> this case is wrong
>
> I know I can do
>
> text[grep(pattern="MY", x=text)][grep(pattern="TARGET",
> x=text[grep(pattern="MY",x=text)])]
>
> However I hope there exists a more elegant way.
Perhaps this?
text[intersect(grep("MY",text), grep("TARGET",text))]
--
O__ ---- Peter Dalgaard ¨ster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list