Re: [R] Matching Problem

From: <Bill.Venables_at_csiro.au>
Date: Wed, 13 Feb 2008 10:18:16 +1000

> sub("^I\\((.*)\\^.*$", "\\1", MyData)
[1] "Test1" "Test2" "Test1" "Test2" "Test1.Test2"

In this case, there is a simple way of discovering which variable names are present, though

> all.vars(parse(text = MyData))
[1] "Test1" "Test2" "Test1.Test2"

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA

Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables_at_csiro.au
http://www.cmis.csiro.au/bill.venables/

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Tom.O
Sent: Tuesday, 12 February 2008 8:44 PM
To: r-help_at_r-project.org
Subject: [R] Matching Problem

Hi

I have this vector of strings.

MyData <-
c("Test1","Test2","I(Test1^2)","I(Test2^3)","I(Test1.Test2^2)") where I want to extract only the text after "I(" and before "^" so that the
string returned only contain c("Test1","Test2","Test1.Test2")

I am not very skilled in the use of matching patterns so bare with me but I
belive I should use gsub('^.\\(', "",MyData) for removing the "I(" and gsub("\\^.+", '',MyData) for the end. but theres got to be a more elegant
way that does the trick in one go.

So I would appriciate I anyone could give me some advice.

Thanks Tom

-- 
View this message in context:
http://www.nabble.com/Matching-Problem-tp15430660p15430660.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.

______________________________________________
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.
Received on Wed 13 Feb 2008 - 00:26:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 13 Feb 2008 - 00:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive