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 -
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
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
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
-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
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
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
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
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:
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.
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
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
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
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?
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))
15 matches
Mail list logo