[R] R-ish challenge for dependent ranking

jim holtman jholtman at gmail.com
Mon Aug 25 18:31:32 CEST 2008


If you want to put it back in the original data frame:

> ds= data.frame( xn=rnorm(32), yn=rnorm(32), zn=rnorm(32) )
> ds$drank1group= as.integer((rank( ds$xn )-1)/4)  # ok, the first set
> ds$drank2group <- ave(ds$yn, ds$drank1group, FUN=rank)
> ds
            xn          yn          zn drank1group drank2group
1  -1.27659221 -0.68166048  0.42510038           0           1
2  -0.57326541 -0.32427027 -0.23864710           1           3
3  -1.22461261  0.06016044  1.05848305           0           3
4  -0.47340064 -0.58889449  0.88642265           2           1
5  -0.62036668  0.53149619 -0.61924305           1           4
6   0.04211587 -1.51839408  2.20610246           5           3
7  -0.91092165  0.30655786 -0.25502703           0           4
8   0.15802877 -1.53644982 -1.42449465           5           2
9  -0.65458464 -0.30097613 -0.14439960           0           2
10  1.76728727 -0.52827990  0.20753834           7           3
11  0.71670748 -0.65209478  2.30797840           6           1
12  0.91017423 -0.05689678  0.10580237           6           3
13  0.38418536 -1.91435943  0.45699881           5           1
14  1.68217608  1.17658331 -0.07715294           7           4
15 -0.63573645 -1.66497244 -0.33400084           1           1
16 -0.46164473 -0.46353040 -0.03472603           2           2
17  1.43228224 -1.11592011  0.78763961           7           1
18 -0.65069635 -0.75081900  2.07524501           1           2
19 -0.20738074  2.08716655  1.02739244           3           4
20 -0.39280793  0.01739562  1.20790840           2           4
21 -0.31999287 -1.28630053 -1.23132342           3           3


On Mon, Aug 25, 2008 at 11:42 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:
> Hi
>
>
> I think you want something like,
>
>> with(ds, tapply(yn, drank1group, rank) )
>
> also, the reshape package should do this sort of thing neatly.
>
> Hope this helps,
>
> baptiste
>
> On 25 Aug 2008, at 16:10, ivo welch wrote:
>
>> Dear R wizards:  First, thanks for the notes on SQL.  These pointers
>> will make it a lot easier to deal with large data sets.   Sorry to
>> have a second short query the same day.  I have been staring at this
>> for a while, but I cannot figure out how to do a dependent ranking the
>> R-sh way.
>>
>> ds= data.frame( xn=rnorm(32), yn=rnorm(32), zn=rnorm(32) )
>> ds$drank1group= as.integer((rank( ds$xn )-1)/4)  # ok, the first set
>> of 8 groups, each with 4 elements
>>
>> ds$drank2.bydrank1group= ??? ## here I want within each drank1group
>> the rank based on yn (from 1 to 4)
>>
>> something like "by(ds,drank1group, rank(ds$yn))".  obviously, this
>> neither works nor has same dimensional output.
>>
>> of course, there is a really simple, clever way to do this in
>> R...except that it totally eludes me.  before I start writing a hand
>> iterating function, could someone please let me know how to do this?
>>
>> regards,
>>
>> /iaw
>>
>> ______________________________________________
>> R-help at 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.
>
> _____________________________
>
> Baptiste Auguié
>
> School of Physics
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
>
> ______________________________________________
> R-help at 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
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list