[R] sub question

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 31 23:09:15 CET 2009


On Sat, Jan 31, 2009 at 4:46 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
> David Hajage wrote:
>> Thank you, it's perfect.
>>
>
> to extend the context, if you were to solve the problem in perl, the
> regex below would work in perl 5.10, but not in earlier versions of
> perl;  another approach is to replace the unwanted leading characters
> with equally many replacement characters at once.
>
> $string = 'aabaab';
>
> # perl 5.10
> $string =~ s/a|(*COMMIT)(*FAIL)/c/g
> # $string is 'ccbaab'
>
> # any recent perl
> $string =~ s/^a*/'c' x length $&/e;
> # $string is 'ccbaab'
>
> i don't know how (if) the latter could be done in r.

This seems quite analogous:

library(gsubfn)
s <- "aabaab"
gsubfn("^a*", ~ paste(rep("c", nchar(x)), collapse = ""), s)[[1]]




More information about the R-help mailing list