[R] Equivalent to regMatchPos in R.

William Dunlap wdunlap at tibco.com
Wed Sep 28 00:04:12 CEST 2011


regexpr() can be used instead of regMatchPos() in S+ and
R.  (It has been in S+ longer than regMatchPos.)

Instead of using regexpr and substring I usually use sub()
or gsub().  E.g., since S+ 8.0 and a long time ago in
R you can do
  x <- c("Cycle 1 Day 15 Hour 2", "Cycle 2 Day 3")
  sub("^.*Day +([[:digit:]]+).*$", "\\1", x)
to get "15" and "3".

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Michael Karol
> Sent: Tuesday, September 27, 2011 2:49 PM
> To: r-help at r-project.org
> Subject: [R] Equivalent to regMatchPos in R.
> 
> R Experts:
> 
> 
> 
> I am trying to isolate the numeric value of day from a string that might
> look like "Cycle 1 Day 8" or "Cycle 12 Day 15".
> 
> 
> 
> In essence, what I need is a function that can look at a character
> string and tell me the location within the string where it matches with
> a given string.  In this case, where within "Cycle 12 Day 15" is the
> text "Day" located.
> 
> 
> 
> The following works in S+, but I haven't been able to locate a function
> within R that does what regMatchPos does in S+.
> 
> (If possible, I'd like a function that works in both.)
> 
> 
> 
> Can someone please help?
> 
> 
> 
> 
> 
> PKTimes$Day <- substring( PKTimes$Visit,
> regMatchPos(PKTimes$Visit,"DAY")[,2] +2, nchar(PKTimes$Visit )
> 
> 
> 
> Thanks,
> 
> Michael
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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