Vis Bandla
Vis Bandla

Reputation: 39

Rank the dataframe values in R

I want to fetch the row numbers of the first 5 highest values in a column in a dataframe and add a value of 100 on the same row number in another dataframe and rest other values as 0.

I know how to sort / order a column in a dataframe using order() function.

df=data.frame(a=c(345,948,290,189,3848,302,384,456,383,201,35,346,1.46,4.66,3,5,63,43,6432,4336,345,354,1245,342,523,743,248,932.5))

For example, df[order(-df$a),] results in

6432.00 4336.00 3848.00 1245.00  948.00  932.50  743.00  523.00  456.00  384.00  383.00  354.00 346.00  345.00  345.00  342.00  302.00  290.00  248.00  201.00  189.00   63.00   43.00   35.00   5.00    4.66    3.00    1.46 

However, I am not able to meet my specific requirement.

I would expect to see df1 as

0 100 0 0 100 0 0 0 0 0 0 0 0 0 0 0 0 0 100 100 0 0 100 0 0 0 0 0

Upvotes: 1

Views: 116

Answers (2)

bouncyball
bouncyball

Reputation: 10771

We could use the rank function:

df$b <- (rank(-df$a) <= 5) * 100

Upvotes: 1

Andrew Cheesman
Andrew Cheesman

Reputation: 150

df$b <- ifelse(df$a %in% sort(df$a, T)[1:5], 100, 0)

Upvotes: 2

Related Questions