Hi, I 'd like to match each member of a list to a target string, e.g. ------------------------------ mylist=c("MN","NY","FL") g=regexpr(mylist[1], "Those from MN:") if (g>0) { "On list" } ------------------------------ My question is: How to add an end-of-string symbol '$' to the to-match string? so that 'M' won't match. Of course, "MN$" will work, but i want to use it in a loop; "mylist[i]" is what i need. I tried "mylist[1]$", but didn't work. So why it doesn't extrapolate? How to do it? Thanks a lot! -- View this message in context: http://www.nabble.com/regexpr-tf4000743.html#a11363041 Sent from the R help mailing list archive at Nabble.com.
mylist=c("MN","NY","FL") g=regexpr(paste(mylist[1], "$", sep=""), "Those from MN:") if (g>0) { "On list" } or in a loop for (i in mylist){ if (regexpr(paste(mylist[i], "$", sep="")) > 0){ .....code for those from.... } } On 6/29/07, runner <sunnyside500@gmail.com> wrote:> > > Hi, > > I 'd like to match each member of a list to a target string, e.g. > ------------------------------ > mylist=c("MN","NY","FL") > g=regexpr(mylist[1], "Those from MN:") > if (g>0) > { > "On list" > } > ------------------------------ > My question is: > > How to add an end-of-string symbol '$' to the to-match string? so that 'M' > won't match. > > Of course, "MN$" will work, but i want to use it in a loop; "mylist[i]" is > what i need. I tried "mylist[1]$", but didn't work. So why it doesn't > extrapolate? How to do it? > > Thanks a lot! > -- > View this message in context: > http://www.nabble.com/regexpr-tf4000743.html#a11363041 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
I think you are looking for paste(). And you can replace your for loop with lapply(), which will apply regexpr to every element of 'mylist' (as the first argument, which is 'pattern'). 'text' can be a vector also: mylist <- c("MN","NY","FL") lapply(paste(mylist,"$",sep=""),regexpr,text="Those from MN:") --- runner <sunnyside500 at gmail.com> wrote:> > Hi, > > I 'd like to match each member of a list to a target string, e.g. > ------------------------------ > mylist=c("MN","NY","FL") > g=regexpr(mylist[1], "Those from MN:") > if (g>0) > { > "On list" > } > ------------------------------ > My question is: > > How to add an end-of-string symbol '$' to the to-match string? so that 'M' > won't match. > > Of course, "MN$" will work, but i want to use it in a loop; "mylist[i]" is > what i need. I tried "mylist[1]$", but didn't work. So why it doesn't > extrapolate? How to do it? > > Thanks a lot! > -- > View this message in context: > http://www.nabble.com/regexpr-tf4000743.html#a11363041 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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 > and provide commented, minimal, self-contained, reproducible code. >____________________________________________________________________________________ Bored stiff? Loosen up...