[R] Matching Problem

Søren Højsgaard Soren.Hojsgaard at agrsci.dk
Tue Feb 12 14:54:01 CET 2008


A way to do it is to use groups (in perl terminology) in connection with regular expressions. My (limited) understanding of it is as follows: Consider
 
> s <-"BBBiiiiEEEeeeeFFFF"
> gsub("BBB(.*)EEE(.*)FFFF", "\\1AAA\\2\\", s)
[1] "iiiiAAAeeee"
> 

The terms in the parentheses are groups which you can refer to with \\1 \\2 etc in the replacement string. So, a solution to your problem could be:
 
> MyData <- c("Test1","Test2","I(Test1^2)","I(Test2^3)","I(Test1.Test2^2)")
> gsub("^I\\((.*)\\^.+", "\\1\\", MyData)
[1] "Test1"       "Test2"       "Test1"       "Test2"       "Test1.Test2"

Now use unique on the result.
 
Regards
Søren
 
 
 

________________________________

Fra: r-help-bounces at r-project.org på vegne af Tom.O
Sendt: ti 12-02-2008 11:44
Til: r-help at r-project.org
Emne: [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.



More information about the R-help mailing list