Manual Ordering

r

#1

Hello All,

I have employee data of a company which has the designation column.
I want to sort this data by designation, but not alphabetically but in some specific order like (Director>Manager>Analyst) using tool R.

Thanks
Nitish


#2

I believe you can do it in excel itself. Here is the general method which you should use whatever the tool is:

  1. Make a table of unique designations and their ranks on which they are to be sorted
  2. Join this table with original data frame and you’ll get a new columns with rank. Joining can be done using vlookup in excel or merge in R.
  3. Sort the data based on this new column added after merging.

Hope this helps.

Cheers,
Aarshay


#3

Hi @nitish_dydx,

Out of the top of the mind, what you can do is make a new column to the designation column, and order according to this new column.

For example, you set

Director -> 1,
Manager -> 2,
Analyst -> 3,

and order by this column.

Hope it helps.


#4

Hi @nitish_dydx,

You just need to specify that the designation field is an ordered factor variable and then you can easily order it.
Refer below example:

> df1 <- data.frame(Id = 1:5, designation = c(“Director”, “Manager”, “Analyst”, “Manager”, “Director”))

> df1
Id designation
1 Director
2 Manager
3 Analyst
4 Manager
5 Director

> df1$designation <- factor(df1$designation,levels = c(“Analyst”, “Manager”, “Director”), ordered = T)

> df1[order(df1$designation), ]
Id designation
3 Analyst
2 Manager
4 Manager
1 Director
5 Director


#5

Thanks sonny. That helps :slight_smile: