Thanks to everyone who answered, in public or in private. I successfully rewrote my function with rle. For my sample, the total computation times (I call this function around 1000 times) are roughly the same. However, I thank you for letting me know rle, and S Poetry. Regards, Nicolas