I have the following data frame in R
DeptNumber EmployeeTypeId
1 10
1 11
1 11
2 23
2 23
2 30
2 40
3 45
3 46
I need to generate another dataframe with a new column MaxEmployeeType
, which should contain the EmployeeTypeId
which is repeated the most for a given DeptNumber
. The output should be as follows
DeptNumber MaxEmployeeType
1 11
2 23
3 45
In case of departmentNumber=3
, there is a tie, but it is ok to present either of the option. I am not sure what is the optimal way to do this? Any help is appreciated.
A similar question is posted already
How to aggregate data in R with mode (most common) value for each row?
but it had a limitation to use only plyr & lubridate. If possible I want a best solution and not limit to these two packages. The question is even down voted possibly due to that it could be homework.