I am trying to calculate the mode for exam grades by ethnicity and the qualification taken, which I have also raised in a previous question. The issue I am having is that the mode function is not performing as it should be. The new column produced by the summarise function, called "mode", just gives a repeat of the OutGrade column. I want a mode for each variation of Ethnicity and Qualification Title. Below are my mode function, and a snippet of my R code
I have tried the use of various mode functions and changed the ordering of my code but no success.
Mode <- function(x) {
uni <- unique(x)
uni[which.max(tabulate(match(x, uni)))]
}
#Ethnic data
eth.data2<-data.comb%>%
group_by(Ethnicity, `Qualification Title`, OutGrade)%>%
summarise(n=n(), mode=Mode(OutGrade))