We 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))
data
df<-data.frame(location=rep(letters[1:3], c(3, 1, 2)),
site = c("site", "site", "bang", "bang", "site", "bang"), stringsAsFactors=FALSE)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…