[R] Regexp subexpression
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Mar 25 18:38:29 CET 2006
We could use sapply to reduce it slightly:
result <- sapply(sprintf("\\%d", 1:2), sub, pattern = pat, x = patid)
result[regexpr(pat, patid) < 0,] <- NA
On 3/25/06, Dieter Menne <dieter.menne at menne-biomed.de> wrote:
> Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:
>
> >
> > In the third case there is no match so there are no
> > substitutions. Handle it separately:
> >
> > pat = "^([[:alpha:]]+)([[:digit:]]+)"
> > result <- cbind(txt = sub(pat, "\\1", patid), num = sub(pat, "\\2", patid))
> > result[regexpr(pat, paid) < 0,] <- NA
> >
>
> Thanks, Gabor, that something like a compressed version of mine. My main
> question was if I was missing something obvious, because I found the double sub
> messy. I am a surprised that there is not
>
> pat = "^([[:alpha:]]+)([[:digit:]]+)"
> mygrep(pat, patid)
>
> returning a list with all subexpressions.
>
> Dieter
>
> ______________________________________________
> 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
>
More information about the R-help
mailing list