[R] Custom Sort Character and Numeric

2011-10-16 Thread swonder03
Im trying to do a custom sort in this order: 1) Numeric digit furthest right; 2) Alphabetical second furthest to the right; 3) Alphabetical the rest of the string beginning with the first character; The example code I'm using is an array that follows: /myArray -

Re: [R] Custom Sort Character and Numeric

2011-10-16 Thread jim holtman
Try this, but I get a different order especially based on the last digit myArray - c('AFP9','AFR9','TLQP7','AFS9','AFR8','AFP8','AFS7','TLQS8') # create a sort key key - sub(^(.*)(.)(.)$, \\3\\2\\1, myArray) key [1] 9PAF 9RAF 7PTLQ 9SAF 8RAF 8PAF 7SAF 8STLQ # sort, but don't get your

Re: [R] Custom Sort Character and Numeric

2011-10-16 Thread jim holtman
Here is another solution that gets the order you posted: myArray - c('AFP9','AFR9','TLQP7','AFS9','AFR8','AFP8','AFS7','TLQS8') # create a sort key key - sub(^(.*)(.)(.)$, \\3\\2\\1, myArray) key [1] 9PAF 9RAF 7PTLQ 9SAF 8RAF 8PAF 7SAF 8STLQ # sort, but don't get your output

[R] Custom Sort on a Table object

2011-06-06 Thread Galen Moore
Greetings - I've got the following table (the result of a two-way table operation): f m 0 to 5 11.328000 6.900901 15 to 24 6.100570 5.190058 25 to 34 9.428707 6.567280 35 to 4410.462158 7.513270 45 to 54 7.621988

Re: [R] Custom Sort on a Table object

2011-06-06 Thread Bill.Venables
-project.org] On Behalf Of Galen Moore Sent: Tuesday, 7 June 2011 1:23 PM To: r-help@r-project.org Subject: [R] Custom Sort on a Table object Greetings - I've got the following table (the result of a two-way table operation): f m 0 to 5 11.328000 6.900901

Re: [R] custom sort?

2009-05-29 Thread Wacek Kusnierczyk
Steve Jaffe wrote: hmm, that is what I was afraid of. I considered that but thought to myself, surely there must be an easier way. I wonder why this feature isn't available. It's there in scripting languages, like perl, but also in hardcore languages like C++ where std::sort and sorted

Re: [R] custom sort?

2009-05-29 Thread Wacek Kusnierczyk
Duncan Murdoch wrote: On 28/05/2009 6:06 PM, Steve Jaffe wrote: hmm, that is what I was afraid of. I considered that but thought to myself, surely there must be an easier way. I wonder why this feature isn't available. It's there in scripting languages, like perl, but also in hardcore

Re: [R] custom sort?

2009-05-29 Thread Wacek Kusnierczyk
Stavros Macrakis wrote: I agree that it is surprising that R doesn't provide a sort function with a comparison function as argument. Perhaps that is partly because calling out to a function for each comparison is relatively expensive; R prefers vector operations. That said, many useful

[R] custom sort?

2009-05-28 Thread Steve Jaffe
Sounds simple but haven't been able to find it in docs: is it possible to sort a vector using a user-defined comparison function? Seems it must be, but sort doesn't seem to provide that option, nor does order sfaics -- View this message in context:

Re: [R] custom sort?

2009-05-28 Thread Duncan Murdoch
On 28/05/2009 5:34 PM, Steve Jaffe wrote: Sounds simple but haven't been able to find it in docs: is it possible to sort a vector using a user-defined comparison function? Seems it must be, but sort doesn't seem to provide that option, nor does order sfaics You put a class on the vector (e.g.

Re: [R] custom sort?

2009-05-28 Thread Steve Jaffe
hmm, that is what I was afraid of. I considered that but thought to myself, surely there must be an easier way. I wonder why this feature isn't available. It's there in scripting languages, like perl, but also in hardcore languages like C++ where std::sort and sorted containers allow the user to

Re: [R] custom sort?

2009-05-28 Thread Stavros Macrakis
I agree that it is surprising that R doesn't provide a sort function with a comparison function as argument. Perhaps that is partly because calling out to a function for each comparison is relatively expensive; R prefers vector operations. That said, many useful custom sorts are easy to define by

Re: [R] custom sort?

2009-05-28 Thread Duncan Murdoch
On 28/05/2009 6:06 PM, Steve Jaffe wrote: hmm, that is what I was afraid of. I considered that but thought to myself, surely there must be an easier way. I wonder why this feature isn't available. It's there in scripting languages, like perl, but also in hardcore languages like C++ where

Re: [R] custom sort?

2009-05-28 Thread Stavros Macrakis
I couldn't get your suggested method to work: `==.foo` - function(a,b) unclass(a)==unclass(b) `.foo` - function(a,b) unclass(a) unclass(b) # invert comparison is.na.foo - function(a)is.na(unclass(a)) sort(structure(sample(5),class=foo)) #- 1:5 -- not reversed What am I missing?

Re: [R] custom sort?

2009-05-28 Thread Duncan Murdoch
On 28/05/2009 8:17 PM, Stavros Macrakis wrote: I couldn't get your suggested method to work: `==.foo` - function(a,b) unclass(a)==unclass(b) `.foo` - function(a,b) unclass(a) unclass(b) # invert comparison is.na.foo - function(a)is.na(unclass(a))