[R] Replace split with regex for speed ?

Henrique Dallazuanna wwwhsd at gmail.com
Fri Mar 18 13:31:51 CET 2011


Try this:

 sub("\\.(\\d+)$", "\\1", ts)


On Thu, Mar 17, 2011 at 11:01 PM, rivercode <aquanyc at gmail.com> wrote:
>
> Have timestamp in format HH:MM:SS.MMM.UUU and need to remove the last "." so
> it is in format HH:MM:SS.MMMUUU.
>
> What is the fastest way to do this, since it has to be repeated on millions
> of rows. Should I use regex ?
>
> Currently doing it with a string split, which is slow:
>
>  >head(ts)
> [1]  09:30:00.000.245  09:30:00.000.256  09:30:00.000.633  09:30:00.001.309
> 09:30:00.003.635  09:30:00.026.370
>
>
>  ts = strsplit(ts, ".", fixed = TRUE)
>  ts=lapply(ts, function(x) { paste(x[1], ".", x[2], x[3], sep="") } )  #
> Remove last . from timestamp, from HH:MM:SS.MMM.UUU to HH:MM:SS.MMMUUU
>  ts = unlist(ts)
>
> Thanks,
> Chris
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Replace-split-with-regex-for-speed-tp3386098p3386098.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list