[R] reverse lexicographic order

Thomas Lumley tlumley at u.washington.edu
Tue Dec 16 02:11:10 CET 2003


On Tue, 16 Dec 2003, Richard A. O'Keefe wrote:

> Here's a strrev() I knocked together quickly:
>
> strrev <-
> function (s) paste(rev(strsplit(s, character(0))[[1]]), collapse="")
>
> If anyone can tell me how to vectorise this, I would be glad of the lesson.
>

strrev<- function(ss) {
	sapply(lapply( strsplit(ss,character(0)), rev), paste, collapse="")
}

vectorises the strsplit() part and should be at least a little faster than
loops for the rev and paste operations.

On the other hand, on my laptop, for 100 copies of the text produced by
license() your version takes about 1.6 seconds and mine takes about 1.5.
If this were to be used a lot it would make sense to code it in C.

	-thomas




More information about the R-help mailing list