Interesting. I know of no practical use for such a function. If the first
position were 'abb,' sub() would return 'aBb,' failing to replace the
second 'b.' I find it hard to believe that's the desired functionality.
Writing a looped regex function in Rcpp makes the most sense for speed.
Using Boost
> b cda
> "aB" "CD" "ef"
>
> By talking instead about simple string matching, I'm afraid you've rather
> hijacked the thread.
>
> -John
>
>
> -John
>
>
> Adam wrote
>
>
>
>
> > I
ate this type of operation.
Cheers,
Adam
> On Jul 30, 2015, at 21:09, John Thaden wrote:
>
>
> Can you show what is its solution for the original sample data? Why that
> discrepancy for you original sub2() function?
>
> From:"Adam Erickson"
> Date:Thu, Jul
uot;
system.time(for(i in 1:5) subCPP(patt, repl, X))
user system elapsed
0.160.000.16
Cheers,
Adam
On Wednesday, July 29, 2015 at 2:42:23 PM UTC-7, Adam Erickson wrote:
>
> Further refining the vectorized (within a loop) exact string match
> function, I get times b
t; "CD" ""
> >
> > Granted, whatever it does, it does it faster
> > #Old method 1
> > system.time(for(i in 1:5)
> > mapply(function(p,r,x) sub(p,r,x, fixed = TRUE),p=patt,r=repl,x=X))
> >user system elapsed
> >2.53
"CD" ""*
>
> Granted, whatever it does, it does it faster
>
> #Old method 1
> system.time(for(i in 1:5)
> mapply(function(p,r,x) sub(p,r,x, fixed = TRUE),p=patt,r=repl,x=X))
>
> *user system elapsed2.530.002.52 *
>
> #Old method 2
&
I know this is an old thread, but I wrote a simple FOR loop with vectorized
pattern replacement that is much faster than either of those (it can also
accept outputs differing in length from the patterns):
sub2 <- function(pattern, replacement, x) {
len <- length(x)
y <- charac
7 matches
Mail list logo