Bal
Bal

Reputation:

Help with the ave() function in R

I'm new to R and l hope anyone can help me with the following:

regarding the command below:

x$rank <- ave(x$fin, x$unique,FUN=rank)

The command works fine, but what i want is the order of x$rank to be reversed, so basically for example the highest value of x$fin is showing its rank as '10', l would like the rank for the highest value to be '1'.

Also can i have more than one field as the second argument (currently x$unique) if there is more than one field to make the row unique. If yes, how would i structure this.

Upvotes: 4

Views: 9100

Answers (1)

shadow
shadow

Reputation: 22303

You can take the rank of -fin to get ranks inverted. If you have several variables to group by, just add the to your arguments:

x$rank <- ave(-x$fin, x$unique1, x$unique2, FUN=rank)

Upvotes: 5

Related Questions