Rank variable by group using Dplyr package in R
In this article, we are going to see how to rank the variable by group using dplyr in R Programming Language.
The dplyr package in R is used to perform mutations and data manipulations in R. It is particularly useful for working with data frames and data tables. The package can be downloaded and installed into the working directory using the following command :
Arranging by rank in ascending order
The arrange() method is invoked to arrange the data of the data frame in the ascending order or descending order. It is used to reorder the rows of the data frame based on the variable names.
This is followed by the application of the group_by method which takes as arguments the set of column names that are used for grouping the data. It may comprise one or more columns.
The mutate() method can then be applied to the data frame to perform manipulations or modifications by changing the orientation of the data stored in the data frame. The rank() method is used to assign numerical rankings to the data values mapped to the groups assigned. By default, the rank is displayed in ascending order.
Arranging by rank in descending order
In order to display the ranks in descending order, the col name is prepended with a minus sign. This displays the numerical ranks of the vector in decreasing order. The new column name can be assigned to the output of this method. The output data frame contains an additional column with the same name as that of the assigned name.