Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

In RStudio, I want to create a rank variable:

The code is as follows:

test <- data.frame(column1 = c(5,5,5,6,6,7,7,7,8))

test$rank <- rank(test)

 test

  column1 rank

1       5  2.0

2       5  2.0

3       5  2.0

4       6  4.5

5       6  4.5

6       7  7.0

7       7  7.0

8       7  7.0

9       8  9.0

My desired answer is: 1,1,1,2,2,3,3,3,4.

1 Answer

0 votes
by (107k points)

In R programming, you can get your desired output with the help of the dense_rank().

test <- data.frame(column1 = c(5,5,5,6,6,7,7,7,8))

test$rank <- dplyr::dense_rank(test$column1)

Working of window ranking function

test %>% rename(input = column1) %>% 

  mutate(row_num_output = row_number(input),

                rank_output = min_rank(input),

                dense_rank_output = dense_rank(input))

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...