[R] reverse lexicographic order

Murray Jorgensen maj at stats.waikato.ac.nz
Mon Dec 15 01:15:05 CET 2003


Hi Duncan, Hi Peter,

thanks for those ideas!  I'm sure I will learn a lot be fooling around 
with them.

Cheers,

Murray

Duncan Murdoch wrote:

> On Mon, 15 Dec 2003 10:08:31 +1300, you wrote:
> 
> 
>>Hi all,
>>
>>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?
> 
> 
> I'm not sure this is what you want, but this function sorts a
> character vector by last letters, then 2nd last, 3rd last, and so on:
> 
> 
> revsort <- function(x,...) {
> 	x[order(unlist(lapply(strsplit(x,''),
> 		function(x) paste(rev(x),collapse=''))),...)]
> }
> 
> 
>>revsort(as.character(1:20))
> 
>  [1] "10" "20" "1"  "11" "2"  "12" "3"  "13" "4"  "14" "5"  "15" "6"
> "16" "7" 
> [16] "17" "8"  "18" "9"  "19"
> 
> The ... args are given to order(), so na.last=FALSE and
> decreasing=TRUE are possibilities.
> 
> Duncan Murdoch
> 
> 
> 
> 

-- 
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz                                Fax 7 838 4155
Phone  +64 7 838 4773 wk    +64 7 849 6486 home    Mobile 021 1395 862




More information about the R-help mailing list