I have a sample data in R.
df <- data.frame(year = c("2020", "2020", "2020", "2020", "2021", "2021", "2021", "2021"), type = c("circle", "circle", "triangle", "star", "circle", "triangle", "star"))
I need to find the mode of type for each year. If type column has same number of values for a year the mode preference will be as following: star > circle > triangle.
So my desired output will be:
2020 : 'circle',
2021 : 'star'
I am trying something similar to this:
mode <- function(codes){
which.max(tabulate(codes))
}
mds <- df %>%
group_by(year) %>%
summarise(mode = mode(type))
This isn't working as type column isn't numeric.