I have the data frame like
now i need to get the output with most frequently occurred levels like
Could any one help me on this please??
See Question&Answers more detail:osI have the data frame like
now i need to get the output with most frequently occurred levels like
Could any one help me on this please??
See Question&Answers more detail:osWe can use Mode
function from here
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
and use it in any of the group by functions. One option is with dplyr
library(dplyr)
df %>%
group_by(location) %>%
summarise(site = Mode(site))
# location site
# <chr> <chr>
#1 a site
#2 b bang
#3 c site
Or with base R
with(df, tapply(as.character(site), location, FUN = Mode))
df<-data.frame(location=rep(letters[1:3], c(3, 1, 2)),
site = c("site", "site", "bang", "bang", "site", "bang"), stringsAsFactors=FALSE)