Anonymous
Anonymous

Reputation: 303

How to rank data from multiple rows and columns?

Example data:

>data.frame("A" = c(20,40,53), "B" = c(40,11,60))

What's the easiest way in R to get from this

   A   B
1 20  40
2 40  11
3 53  60

to this?

  A       B
1 2.0   3.5
2 3.5   1.0
3 5.0   6.0

I couldn't find a way to make rank() or frank() work on multiple rows/columns and googling things like "r rank dataframe" "r rank multiple rows" yielded only questions on how to rank multiple rows/columns individually, which is weird, as I suspect the question must have been answered before.

Upvotes: 0

Views: 74

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101343

Try rank like below

df[] <- rank(df)

or

df <- list2DF(relist(rank(df),skeleton = unclass(df)))

and you will get

> df
    A   B
1 2.0 3.5
2 3.5 1.0
3 5.0 6.0

Upvotes: 4

Related Questions