Some benchmarking and a faster solution. rep.int is a faster implementation of rep in a standard use case (from ?rep )
rep.int(a, b)
I was not sure about the benchmarking above
inverse.rle is just a wrapper for rep.int . rep.int should be faster than rep . I think the wrapper inverse.rle component should be slower than interpreting rep() as a primitive function
Some micro lenses
library(microbenchmark) microbenchmark(rep(a,b), rep.int(a,b), inverse.rle(list(values = a, lengths =b))) Unit: milliseconds expr min lq median uq 1 inverse.rle(list(values = a, lengths = b)) 29.06968 29.26267 29.36191 29.67501 2 rep(a, b) 25.65125 25.76246 25.84869 26.52348 3 rep.int(a, b) 20.38604 23.31840 23.38940 23.69600 max 1 72.80645 2 69.00169 3 66.40759
There is little in it, but the rep.int appears to rep.int - which he owes.
source share