Dear All, I would appreciate any help with the following: given the vector 'x' x <- c("Ass1", "Ass.s1", "Ass2", "Ass.s2") I would like to pick up the positions where the character string contains "Ass" but does not contain "Ass.s", so for 'x' that would be positions 1 and 3. I guess this could be programmed around grep() using a suitable regular expression, but I haven't managed to succeed. Thanks in advance. Best, Dimitris -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
On Nov 7, 2010, at 10:15 AM, Dimitris Rizopoulos wrote:> Dear All, > > I would appreciate any help with the following: given the vector 'x' > > x <- c("Ass1", "Ass.s1", "Ass2", "Ass.s2") > > I would like to pick up the positions where the character string > contains "Ass" but does not contain "Ass.s", so for 'x' that would > be positions 1 and 3.> x[ grepl("Ass", x) & !grepl("Ass\\.s", x) ] [1] "Ass1" "Ass2" HTH; -- David> > I guess this could be programmed around grep() using a suitable > regular expression, but I haven't managed to succeed. > > Thanks in advance. > > Best, > Dimitris > > -- > Dimitris Rizopoulos > Assistant ProfessorDavid Winsemius, MD West Hartford, CT
Try this: grep("Ass[^\\.]", x) On Sun, Nov 7, 2010 at 1:15 PM, Dimitris Rizopoulos < d.rizopoulos@erasmusmc.nl> wrote:> Dear All, > > I would appreciate any help with the following: given the vector 'x' > > x <- c("Ass1", "Ass.s1", "Ass2", "Ass.s2") > > I would like to pick up the positions where the character string contains > "Ass" but does not contain "Ass.s", so for 'x' that would be positions 1 and > 3. > > I guess this could be programmed around grep() using a suitable regular > expression, but I haven't managed to succeed. > > Thanks in advance. > > Best, > Dimitris > > -- > Dimitris Rizopoulos > Assistant Professor > Department of Biostatistics > Erasmus University Medical Center > > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands > Tel: +31/(0)10/7043478 > Fax: +31/(0)10/7043014 > Web: http://www.erasmusmc.nl/biostatistiek/ > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Sun, Nov 7, 2010 at 10:15 AM, Dimitris Rizopoulos <d.rizopoulos at erasmusmc.nl> wrote:> Dear All, > > I would appreciate any help with the following: given the vector 'x' > > x <- c("Ass1", "Ass.s1", "Ass2", "Ass.s2") > > I would like to pick up the positions where the character string contains > "Ass" but does not contain "Ass.s", so for 'x' that would be positions 1 and > 3. > > I guess this could be programmed around grep() using a suitable regular > expression, but I haven't managed to succeed. >Assuming that "Ass" appears at most once in each string, as in the example:> x <- c("Ass1", "Ass.s1", "Ass2", "Ass.s2", "Ass.") > grep("Ass(?![.]s)", x, perl = TRUE)[1] 1 3 5 See ?regex for more info. There is also regular expression links in the Links box at http://gsubfn.googlecode.com . -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com