[R] reverse lexicographic order

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Dec 16 05:41:17 CET 2003


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

Thomas Lumley <tlumley at u.washington.edu> suggested
	strrev<- function(ss) {
	    sapply(lapply( strsplit(ss,character(0)), rev), paste, collapse="")
	}
	
Unfortunately, I failed to explain myself clearly, so this doesn't actually
answer the question I _meant_ to ask.  For me, sticking in some variant of
'apply' means you have _failed_ to vectorise.  The string reversal code in
?rev doesn't count for the same reason.

There is no reason why a built-in strrev() couldn't be as vectorised as
most built-ins, it's just not common enough to deserve a lot of effort.




More information about the R-help mailing list