# Rank Function in R

rank() function in R returns the ranks of the values in a vector. rank function in R also handles Ties and missing values in several ways. Rank of the vector with NA. Min rank, Max rank, last rank and average rank in R. rank() function in R returns the rank of the column in R. We can also calculate minimum and maximum rank of the column in R dataframe.

• Rank the Vector in R by descending order, by minimum rank, maximum rank, first rank, last rank and average of two ranks if two values are found same
• Rank the dataframe in R by ascending and descending order
• Rank the dataframe column by minimum rank if found 2 values are same
• Rank the dataframe by Maximum rank if found 2 values are same
• Rank the dataframe column by first,  last and average of two rank if found 2 values are same
• Rank the dataframe of the character column in R using rank() function.

#### Syntax for rank function in R:

rank(x, na.last = TRUE, ties.method = c(“average”, “first”, “random”, “max”, “min”))
 x numeric, character or logical vector na.last Treatment of NAs. How to Handle NAs ties.method Treatment of Ties. How to Handle Ties

#### Rank function in R with NAs as last:

```x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x)
```

by default NAs are ranked last, so the output will be

  3  4  2  1  8  7  6  5

#### Rank function in R with NAs as First:

NAs are ranked first

```x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = FALSE) #NA FIRST
```
##### output:
  4  5  3  2  1  8  7  6

#### Rank function in R with NAs are removed:

NAs are neglectled by rank function

```x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = NA) #NA removed
```
##### output:
  3  4  2  1  7  6  5

#### Rank a vector in R with NAs are ranked as NA:

NAs are kept and ranked as NAs by rank function

```x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = "keep") #NA is kept with rank NA
```
##### output:
  3  4  2  1 NA  7  6  5

#### Rank function in R with Ties=”Average”:

Average of the rank is allocated to the elements of vector when ties are encountered

```x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "first")
```
##### output:
 3.0   4.5   2.0   1.0   9.0   8.0   7.0   6.0   4.5

#### Rank function in R with Ties=”random”:

```x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "random")
```
##### output:
 3  4  2  1  9  8  7  6  5

#### Rank function in R with Ties=”max”:

Maximum value of the rank is allocated to the elements of vector when ties are encountered

```x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "max")
```
##### output:
  3  5  2  1  9  8  7  6  5

#### Rank function in R with Ties=”min”:

Minimum value of the rank is allocated to the elements of vector when ties are encountered

```x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "min")
```
##### output:
  3  4  2  1  9  8  7  6  4

#### Ranking of character vector in R:

Rank() function can also be used to rank character vector

```y<-c("a","h","e","d")
rank(y)
```
  1  4  3  2

#### Rank the dataframe columns in R

First lets create the dataframe as shown below

```#### Create dataframe
Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120))

```

so the resultant dataframe will be #### Rank the dataframe column in ascending order R

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, average rank of both the values are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be

#### Rank the dataframe column in descending order R

Rank of “Price” column in desending order is calculated and assigned to the column “Price_rank”. in case of same values, average rank of both the values are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be

#### Rank the dataframe column in ascending order with random ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, random rank of both the values are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be #### Rank the dataframe column in ascending order with max ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, maximum rank of both the values in case of ties  are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be #### Rank the dataframe column in ascending order with min ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, minimum rank of both the values in case of ties  are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be #### Rank the dataframe column in ascending order with First ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, First rank is assigned to the first value in case of ties  are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be #### Rank the dataframe column in ascending order with last ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, Last rank is assigned to the first value in case of ties  are assigned as shown below.

```###### Rank the dataframe column in R

```

so the resultant dataframe with rank calculated will be #### Rank the character column of the dataframe  in R

Rank of “ITEM_NAME” column in ascending order is calculated and assigned to the column “ITEM_NAME_ALphabet_RANK”.

```###### Rank the dataframe column in R 