[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 - c('AFP9','AFR9','TLQP7','AFS9','AFR8','AFP8','AFS7','TLQS8')/

The output I desire is:

/myArray
[1] AFS7 AFP8 AFR8  AFP9 AFR9 AFS9 TLQP7 TLQS8  /

What I'm thinking is writing a function that will order it by analyzing it
from right to left. Ideally there would be a way to look at the individual
strings like the formula in Excel =RIGHT(cell, 1) and drop the furthest
right then do the same thing to the next character. I've been looking into
custom sort for R and haven't found much. Any idea what this function would
look like? Possibly a while loop? Each string would have a length of at
least 3, possibly longer. Thank you in advance. 



--
View this message in context: 
http://r.789695.n4.nabble.com/Custom-Sort-Character-and-Numeric-tp3909058p3909058.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 output
 myArray[order(key)]
[1] TLQP7 AFS7  AFP8  AFR8  TLQS8 AFP9  AFR9  AFS9



On Sun, Oct 16, 2011 at 4:47 AM, swonder03 ramey.ste...@gmail.com wrote:
 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 - c('AFP9','AFR9','TLQP7','AFS9','AFR8','AFP8','AFS7','TLQS8')/

 The output I desire is:

 /myArray
 [1] AFS7 AFP8 AFR8  AFP9 AFR9 AFS9 TLQP7 TLQS8  /

 What I'm thinking is writing a function that will order it by analyzing it
 from right to left. Ideally there would be a way to look at the individual
 strings like the formula in Excel =RIGHT(cell, 1) and drop the furthest
 right then do the same thing to the next character. I've been looking into
 custom sort for R and haven't found much. Any idea what this function would
 look like? Possibly a while loop? Each string would have a length of at
 least 3, possibly longer. Thank you in advance.



 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Custom-Sort-Character-and-Numeric-tp3909058p3909058.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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
 myArray[order(key)]
[1] TLQP7 AFS7  AFP8  AFR8  TLQS8 AFP9  AFR9  AFS9
 # to get your output, new key
 newKey - sub(^(.*)(.)(.)$, \\1\\3\\2, myArray)
 newKey
[1] AF9P  AF9R  TLQ7P AF9S  AF8R  AF8P  AF7S  TLQ8S
 myArray[order(newKey)]
[1] AFS7  AFP8  AFR8  AFP9  AFR9  AFS9  TLQP7 TLQS8



On Sun, Oct 16, 2011 at 4:47 AM, swonder03 ramey.ste...@gmail.com wrote:
 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 - c('AFP9','AFR9','TLQP7','AFS9','AFR8','AFP8','AFS7','TLQS8')/

 The output I desire is:

 /myArray
 [1] AFS7 AFP8 AFR8  AFP9 AFR9 AFS9 TLQP7 TLQS8  /

 What I'm thinking is writing a function that will order it by analyzing it
 from right to left. Ideally there would be a way to look at the individual
 strings like the formula in Excel =RIGHT(cell, 1) and drop the furthest
 right then do the same thing to the next character. I've been looking into
 custom sort for R and haven't found much. Any idea what this function would
 look like? Possibly a while loop? Each string would have a length of at
 least 3, possibly longer. Thank you in advance.



 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Custom-Sort-Character-and-Numeric-tp3909058p3909058.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[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  5.692905

  5 to 14  6.502741  6.119663

  55 to 64 5.884737  4.319905

  65 to 74 5.075606  4.267810

  75 to 84 4.702020  3.602362

  85 and over  4.75  3.877551

 

Which I'd like to sort so that the column of rownames (which represent age
bands) and their corresponding f and m values appear in logical order.  I've
tried a bunch of things; merging with a separate df bearing Age Bands paired
with a sequence number, stripping out row vectors and rbind-ing a new df,
etc., all to no avail.  It seems to be very difficult to spin a table object
into a data frame without being stuck with the tables rownames!

 

I haven't yet tried writing to an external file and then reading it back (so
as to get R to forget that it's a Table object), and then merging on the
group bands to pull in a sequence vector upon which to do an order().  Seems
like it should be easier. 

 

Many thanks,

 

Galen 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Custom Sort on a Table object

2011-06-06 Thread Bill.Venables
Here is a one way.

 tab
fm
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 5.692905
5 to 14  6.502741 6.119663
55 to 64 5.884737 4.319905
65 to 74 5.075606 4.267810
75 to 84 4.702020 3.602362
85 and_over  4.75 3.877551
 
 lowAge - as.numeric(sapply(strsplit(rownames(tab), ), [, 1))
 (tab - tab[order(lowAge), ])
fm
0 to 5  11.328000 6.900901
5 to 14  6.502741 6.119663
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 5.692905
55 to 64 5.884737 4.319905
65 to 74 5.075606 4.267810
75 to 84 4.702020 3.602362
85 and over  4.75 3.877551
  

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-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

  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  5.692905

  5 to 14  6.502741  6.119663

  55 to 64 5.884737  4.319905

  65 to 74 5.075606  4.267810

  75 to 84 4.702020  3.602362

  85 and over  4.75  3.877551

 

Which I'd like to sort so that the column of rownames (which represent age
bands) and their corresponding f and m values appear in logical order.  I've
tried a bunch of things; merging with a separate df bearing Age Bands paired
with a sequence number, stripping out row vectors and rbind-ing a new df,
etc., all to no avail.  It seems to be very difficult to spin a table object
into a data frame without being stuck with the tables rownames!

 

I haven't yet tried writing to an external file and then reading it back (so
as to get R to forget that it's a Table object), and then merging on the
group bands to pull in a sequence vector upon which to do an order().  Seems
like it should be easier. 

 

Many thanks,

 

Galen 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 containers allow
 the user to provide a comparison function (even for builtin types like int).
 It's hard to believe that you have to jump through more hoops to do a custom
 sort in R than in C++ ...


 You put a class on the vector...
   

there has been some discussion on sorting here some time ago;  r folks
do not seem to accept the idea of sorting as a generic task, with the
sorting algorithm a separate thing from the comparison operator and the
type of the elements to be sorted.  (hence you can't sort arbitrary
lists, for example.) 

you may want to lookup the archives.

vQ

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 languages like C++ where std::sort and sorted containers
 allow
 the user to provide a comparison function (even for builtin types
 like int).
 It's hard to believe that you have to jump through more hoops to do a
 custom
 sort in R than in C++ ...


 You put a class on the vector...


 Each of the things I described to you would take about a line of code,
 plus the logic of the comparisons.  So the hoops R makes you jump
 through amount to 2-4 extra lines of code.  C++  is probably going to
 take at least that much.

it's not just about the amount of code.  defining a comparator is
conceptually different from defining a class to sort, e.g., regular
numbers or strings.  passing an additional comparator argument to a
sorting routine is more convenient and readable than wrapping an object
into a dummy class for the mere purpose of sorting it. 

You put a class on the vector (e.g. using class(x) - myvector), then
define a conversion to numeric (e.g. xtfrm.myvector) or actual
comparison methods (you'll need ==.myvector, .myvector, and
is.na.myvector). 

x = 3:1
class(x) = 'foo'

'==.foo' = function(...) FALSE
'.foo' = function(...) FALSE
sort(x)
# 1 2 3  

'==.foo' = function(...) TRUE
'.foo' = function(...) TRUE
sort(x)
# 1 2 3  

'==.foo' = function(...) TRUE
'.foo' = function(...) FALSE
sort(x)
# 1 2 3  

'==.foo' = function(...) FALSE
'.foo' = function(...) TRUE
sort(x)
# 1 2 3  

hmm, doesn't seem to work.  it also won't do for the case when lists
('generic vectors') are to be sorted:

x = structure(list(1,2,3), class='foo')
sort(x)
# Error in switch(ties.method, average = , min = , max =
.Internal(rank(x[!nas],  :
#   unimplemented type 'list' in 'greater'

  
vQ

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 custom sorts are easy to define by reordering,
 possibly using the 'order' function, e.g.

 rr - function (v) v[order( v %% 10 , v  500, - v ) ]
 # sort first by last digit (ascending), then by whether  500, then by
 magnitude (descending)

   

duncan's suggestion (then define a conversion to numeric) and your
suggestion (using the 'order' function) can be combined to what could
be considered a form of the schwartzian tranform, e.g.:

stsort = function(x, transform, ...)
   x[order(transform(x), ...)]

stsort(1:3, `-`)
# 3 2 1
stsort(c('foo', 'f', 'fo'), nchar)
# f fo foo
...

vQ

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[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: 
http://www.nabble.com/custom-sort--tp23770565p23770565.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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. using class(x) - myvector), then 
define a conversion to numeric (e.g. xtfrm.myvector) or actual 
comparison methods (you'll need ==.myvector, .myvector, and 
is.na.myvector).


Duncan Murdoch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 provide a comparison function (even for builtin types like int).
It's hard to believe that you have to jump through more hoops to do a custom
sort in R than in C++ ...


You put a class on the vector...

-- 
View this message in context: 
http://www.nabble.com/custom-sort--tp23770565p23770964.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 reordering,
possibly using the 'order' function, e.g.

rr - function (v) v[order( v %% 10 , v  500, - v ) ]
# sort first by last digit (ascending), then by whether  500, then by
magnitude (descending)

set.seed(2009)
rr(sample(1000,30))
 [1] 840 670 580 140 100  10 991 901 881 561 231  71 722 662 432 222  32
473  53
[20]  24 645 796  86 697 607 567 397 257  77 818 568 428 198 619 569 479 439
299

Hope this helps,

 -s

On Thu, May 28, 2009 at 6:06 PM, Steve Jaffe sja...@riskspan.com 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 containers allow
 the user to provide a comparison function (even for builtin types like
 int).
 It's hard to believe that you have to jump through more hoops to do a
 custom
 sort in R than in C++ ...


 You put a class on the vector...

 --
 View this message in context:
 http://www.nabble.com/custom-sort--tp23770565p23770964.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 std::sort and sorted containers allow
the user to provide a comparison function (even for builtin types like int).
It's hard to believe that you have to jump through more hoops to do a custom
sort in R than in C++ ...


You put a class on the vector...



Each of the things I described to you would take about a line of code, 
plus the logic of the comparisons.  So the hoops R makes you jump 
through amount to 2-4 extra lines of code.  C++  is probably going to 
take at least that much.


Duncan Murdoch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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?

   -s

On Thu, May 28, 2009 at 5:48 PM, Duncan Murdoch murd...@stats.uwo.cawrote:

 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. using class(x) - myvector), then
 define a conversion to numeric (e.g. xtfrm.myvector) or actual comparison
 methods (you'll need ==.myvector, .myvector, and is.na.myvector).

 Duncan Murdoch


 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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))

  sort(structure(sample(5),class=foo))  #- 1:5  -- not reversed

What am I missing?


I'm not sure:  I've never actually used that, I was basing it on the 
docs.  Looks like it's not as simple as I thought.  Sorry for the 
misinformation.


Duncan Murdoch



   -s

On Thu, May 28, 2009 at 5:48 PM, Duncan Murdoch murd...@stats.uwo.cawrote:


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. using class(x) - myvector), then
define a conversion to numeric (e.g. xtfrm.myvector) or actual comparison
methods (you'll need ==.myvector, .myvector, and is.na.myvector).

Duncan Murdoch


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.