RANK variables

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

RANK variables

oggesjolin
Hi!

I need to rank values (in say 3 variables) given by every ID on a row basis, I have data like this:

ID, q1, q2, q3
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50

I want 3 ranking variables created (highest value among the 3 vars as rank1 and so on), resulting in:

ID, q1, q2, q3, rank_q1, rank_q2, rank_q3
1, 15, 10, 5, 1, 2, 3
2, 10, 20, 40, 3, 2, 1
3, 20, 10, 50, 2, 3, 1

How would one go about this?

Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

David Marso
Administrator
Easy Peasy!!!
DATA LIST FREE /ID  q1  q2  q3 .
BEGIN DATA
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50
END DATA.

VARSTOCASES / MAKE q FROM q1 TO q3.
RANK VARIABLES =q (D) BY ID .
CASESTOVARS / ID=ID.

FORMATS ALL (F2.0).
LIST.
 
 
ID q.1 q.2 q.3 Rq.1 Rq.2 Rq.3
 
 1  15  10   5   1    2    3
 2  10  20  40   3    2    1
 3  10  20  50   3    2    1

Number of cases read:  3    Number of cases listed:  3


oggesjolin wrote
Hi!

I need to rank values (in say 3 variables) given by every ID on a row basis, I have data like this:

ID, q1, q2, q3
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50

I want 3 ranking variables created (highest value among the 3 vars as rank1 and so on), resulting in:

ID, q1, q2, q3, rank_q1, rank_q2, rank_q3
1, 15, 10, 5, 1, 2, 3
2, 10, 20, 40, 3, 2, 1
3, 20, 10, 50, 2, 3, 1

How would one go about this?
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

Jon Peck
Without the violence of varstocases and casestovars:

DATA LIST FREE /ID  q1  q2  q3 .
BEGIN DATA
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50
END DATA.

spssinc trans result=q1 q2 q3
/formula "sorted([q1,q2,q3])".

Using the SPSSINC TRANS extension command normally installed with Statistics.

On Thu, Feb 11, 2016 at 8:05 AM, David Marso <[hidden email]> wrote:
Easy Peasy!!!
DATA LIST FREE /ID  q1  q2  q3 .
BEGIN DATA
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50
END DATA.

*VARSTOCASES / MAKE q FROM q1 TO q3.
RANK VARIABLES =q (D) BY ID .
CASESTOVARS / ID=ID.*
FORMATS ALL (F2.0).
LIST.


ID q.1 q.2 q.3 Rq.1 Rq.2 Rq.3

 1  15  10   5   1    2    3
 2  10  20  40   3    2    1
 3  10  20  50   3    2    1

Number of cases read:  3    Number of cases listed:  3



oggesjolin wrote
> Hi!
>
> I need to rank values (in say 3 variables) given by every ID on a row
> basis, I have data like this:
>
> ID, q1, q2, q3
> 1, 15, 10, 5
> 2, 10, 20, 40
> 3, 10, 20, 50
>
> I want 3 ranking variables created (highest value among the 3 vars as
> rank1 and so on), resulting in:
>
> ID, q1, q2, q3, rank_q1, rank_q2, rank_q3
> 1, 15, 10, 5, 1, 2, 3
> 2, 10, 20, 40, 3, 2, 1
> 3, 20, 10, 50, 2, 3, 1
>
> How would one go about this?





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/RANK-variables-tp5731501p5731503.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

Bruce Weaver
Administrator
Jon, your syntax is SORTING the values within each case (from lowest to highest):

      ID       q1       q2       q3
 
    1.00     5.00    10.00    15.00
    2.00    10.00    20.00    40.00
    3.00    10.00    20.00    50.00

But the OP wants the RANKS for Q1-Q3 within each case, with Rank=1 for the highest value.  Can SPSSINC TRANS be tweaked to do that?




Jon Peck wrote
Without the violence of varstocases and casestovars:

DATA LIST FREE /ID  q1  q2  q3 .
BEGIN DATA
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50
END DATA.

spssinc trans result=q1 q2 q3
/formula "sorted([q1,q2,q3])".

Using the SPSSINC TRANS extension command normally installed with
Statistics.

On Thu, Feb 11, 2016 at 8:05 AM, David Marso <[hidden email]> wrote:

> Easy Peasy!!!
> DATA LIST FREE /ID  q1  q2  q3 .
> BEGIN DATA
> 1, 15, 10, 5
> 2, 10, 20, 40
> 3, 10, 20, 50
> END DATA.
>
> *VARSTOCASES / MAKE q FROM q1 TO q3.
> RANK VARIABLES =q (D) BY ID .
> CASESTOVARS / ID=ID.*
> FORMATS ALL (F2.0).
> LIST.
>
>
> ID q.1 q.2 q.3 Rq.1 Rq.2 Rq.3
>
>  1  15  10   5   1    2    3
>  2  10  20  40   3    2    1
>  3  10  20  50   3    2    1
>
> Number of cases read:  3    Number of cases listed:  3
>
>
>
> oggesjolin wrote
> > Hi!
> >
> > I need to rank values (in say 3 variables) given by every ID on a row
> > basis, I have data like this:
> >
> > ID, q1, q2, q3
> > 1, 15, 10, 5
> > 2, 10, 20, 40
> > 3, 10, 20, 50
> >
> > I want 3 ranking variables created (highest value among the 3 vars as
> > rank1 and so on), resulting in:
> >
> > ID, q1, q2, q3, rank_q1, rank_q2, rank_q3
> > 1, 15, 10, 5, 1, 2, 3
> > 2, 10, 20, 40, 3, 2, 1
> > 3, 20, 10, 50, 2, 3, 1
> >
> > How would one go about this?
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/RANK-variables-tp5731501p5731503.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>



--
Jon K Peck
[hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

Jon Peck
I misunderstood.  Here's the solution for ranks.  I put the result in new variables to make checking easier, but the result could have just overwritten.  If there can be ties, some additional logic would be required depending on what the OP wants.

DATA LIST FREE /ID  q1  q2  q3 .
BEGIN DATA
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50
END DATA.

begin program.
def index(*args):
    args2 = sorted(args, reverse=True)
    return [args2.index(v)+1 for v in args]
end program.

spssinc trans result=q4 q5 q6
/formula "index(q1, q2, q3)".

This is the dataset afterwards.
1.00 15.00 10.00 5.00 1.00 2.00 3.00
2.00 10.00 20.00 40.00 3.00 2.00 1.00
3.00 10.00 20.00 50.00 3.00 2.00 1.00

On Thu, Feb 11, 2016 at 9:07 AM, Bruce Weaver <[hidden email]> wrote:
Jon, your syntax is SORTING the values within each case (from lowest to
highest):

      ID       q1       q2       q3

    1.00     5.00    10.00    15.00
    2.00    10.00    20.00    40.00
    3.00    10.00    20.00    50.00

But the OP wants the RANKS for Q1-Q3 within each case, with Rank=1 for the
highest value.  Can SPSSINC TRANS be tweaked to do that?





Jon Peck wrote
> Without the violence of varstocases and casestovars:
>
> DATA LIST FREE /ID  q1  q2  q3 .
> BEGIN DATA
> 1, 15, 10, 5
> 2, 10, 20, 40
> 3, 10, 20, 50
> END DATA.
>
> spssinc trans result=q1 q2 q3
> /formula "sorted([q1,q2,q3])".
>
> Using the SPSSINC TRANS extension command normally installed with
> Statistics.
>
> On Thu, Feb 11, 2016 at 8:05 AM, David Marso <

> david.marso@

> > wrote:
>
>> Easy Peasy!!!
>> DATA LIST FREE /ID  q1  q2  q3 .
>> BEGIN DATA
>> 1, 15, 10, 5
>> 2, 10, 20, 40
>> 3, 10, 20, 50
>> END DATA.
>>
>> *VARSTOCASES / MAKE q FROM q1 TO q3.
>> RANK VARIABLES =q (D) BY ID .
>> CASESTOVARS / ID=ID.*
>> FORMATS ALL (F2.0).
>> LIST.
>>
>>
>> ID q.1 q.2 q.3 Rq.1 Rq.2 Rq.3
>>
>>  1  15  10   5   1    2    3
>>  2  10  20  40   3    2    1
>>  3  10  20  50   3    2    1
>>
>> Number of cases read:  3    Number of cases listed:  3
>>
>>
>>
>> oggesjolin wrote
>> > Hi!
>> >
>> > I need to rank values (in say 3 variables) given by every ID on a row
>> > basis, I have data like this:
>> >
>> > ID, q1, q2, q3
>> > 1, 15, 10, 5
>> > 2, 10, 20, 40
>> > 3, 10, 20, 50
>> >
>> > I want 3 ranking variables created (highest value among the 3 vars as
>> > rank1 and so on), resulting in:
>> >
>> > ID, q1, q2, q3, rank_q1, rank_q2, rank_q3
>> > 1, 15, 10, 5, 1, 2, 3
>> > 2, 10, 20, 40, 3, 2, 1
>> > 3, 20, 10, 50, 2, 3, 1
>> >
>> > How would one go about this?
>>
>>
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> View this message in context:
>> http://spssx-discussion.1045642.n5.nabble.com/RANK-variables-tp5731501p5731503.html
>> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
>> command. To leave the list, send the command
>> SIGNOFF SPSSX-L
>> For a list of commands to manage subscriptions, send the command
>> INFO REFCARD
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD





-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/RANK-variables-tp5731501p5731507.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

Kirill Orlov
In reply to this post by oggesjolin
Horizontal ranking is available as a macro found on my page http://www.spsstools.net/en/KO-spssmacros


11.02.2016 17:51, oggesjolin пишет:
Hi!

I need to rank values (in say 3 variables) given by every ID on a row basis,
I have data like this:

ID, q1, q2, q3
1, 15, 10, 5
2, 10, 20, 40
3, 10, 20, 50

I want 3 ranking variables created (highest value among the 3 vars as rank1
and so on), resulting in:

ID, q1, q2, q3, rank_q1, rank_q2, rank_q3
1, 15, 10, 5, 1, 2, 3
2, 10, 20, 40, 3, 2, 1
3, 20, 10, 50, 2, 3, 1 

How would one go about this?





--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/RANK-variables-tp5731501.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD




===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

oggesjolin
In reply to this post by Jon Peck
Thanks Jon! I need to sort out how to handle ties though, in the case with 3 vars, if 2 were tied as rank 2, I think they should both rank as 2, and there should be no rank 3 and so on.. I'll try and read up on it. Thanks again!

Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

David Marso
Administrator
oggesjolin wrote
Thanks Jon! I need to sort out how to handle ties though, in the case with 3 vars, if 2 were tied as rank 2, I think they should both rank as 2, and there should be no rank 3 and so on.. I'll try and read up on it. Thanks again!
I have a feeling adapting python code for that might involve some 'fun times'.
Pardon the violence and bloodshed of VARSTOCASES.  Cover your eyes if necessary ;-)
I can hear the data screaming ...New blockbuster movie The Silence of the Data...
My earlier code adapted for LOW ties:  See FM under RANK for behavior of other TIE options (HIGH, MEAN,CONDENSE).
DATA LIST FREE /ID  q1  q2  q3 q4.
BEGIN DATA
1, 15, 10, 5 ,10
2, 10, 20, 40 ,20
3, 10, 20, 50 ,50
END DATA.

VARSTOCASES / MAKE q FROM q1 TO q4.
RANK VARIABLES =q (D) BY ID /TIES=LOW .
CASESTOVARS / ID=ID.
FORMATS ALL (F2.0).
LIST.
 
 
ID q.1 q.2 q.3 q.4 Rq.1 Rq.2 Rq.3 Rq.4
 
 1  15  10   5  10   1    2    4    2
 2  10  20  40  20   4    2    1    2
 3  10  20  50  50   4    3    1    1


Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

oggesjolin
In reply to this post by Jon Peck
Hi Jon!

To further request help on this... I need ties to ranked just as in scipy's Rankdata (I think). Or like MS Excel's RANK.AVG function. Is that possible with SPSSINC TRANS=?

/o
Reply | Threaded
Open this post in threaded view
|

Re: RANK variables

Jon Peck
The Excel rank.avg function in the case of ties returns the average rank of all the ties for each value.
Change the begin program block like this to do the same.  This will work for any number of input variables

begin program.
def index(*args):
    slist = sorted(args, reverse=True)
    result = []

    for j in range(len(args)):
        ranks = [i+1 for i in range(len(slist)) if slist[i] == args[j]]
        result.append(float(sum(ranks))/len(ranks))
    return result
end program.

On Thu, Feb 25, 2016 at 3:54 AM, oggesjolin <[hidden email]> wrote:
Hi Jon!

To further request help on this... I need ties to ranked just as in scipy's
Rankdata (I think). Or like MS Excel's RANK.AVG function. Is that possible
with SPSSINC TRANS=?

/o



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/RANK-variables-tp5731501p5731612.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD