[R] reverse lexicographic order

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Dec 16 01:29:39 CET 2003


Dr Murray Jorgensen (maj at waikato.ac.nz) wrote:
	> I have some email addresses that I would like to sort in reverse
	> lexicographic order so that addresses from the same domain will be
	> grouped together. How might that be done?

Because he wants addresses from the same domain to be grouped together
(so that foo.bar.ick.ac should be in the same group as zoo.sno.ick.ac),
it is not sufficient to split at the at-sign.

The obvious method is
(1) reverse the strings
(2) sort the reversed strings
(3) reverse the sorted reversed strings

All of this is obvious except reversing the strings.
There's ?rev, which reverses vectors, but no strrev.

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.




More information about the R-help mailing list