获取数据框中最频繁的级别

时间:2016-06-21 12:10:15

标签: r

我有像

这样的数据框

enter image description here

现在我需要获得最常出现的水平的输出,如

enter image description here

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

你可以用dplyr

做这样的事情
public class LoggedOrAuthorizedAttribute : AuthorizeAttribute 
{ 
   public LoggedOrAuthorizedAttribute() 
    { 
       View = "error"; 
       Master = String.Empty; 
    } 

    public String View { get; set; } 
    public String Master { get; set; } 

public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
  base.OnAuthorization(filterContext); 
  CheckIfUserIsAuthenticated(filterContext); 
} 

   private void CheckIfUserIsAuthenticated(AuthorizationContext filterContext) 
{ 
   // If Result is null, we’re OK: the user is authenticated and authorized. 
   if (filterContext.Result == null) 
      return; 

   // If here, you’re getting an HTTP 401 status code. In particular,
   // filterContext.Result is of HttpUnauthorizedResult type. Check Ajax      here. 
   if (filterContext.HttpContext.User.Identity.IsAuthenticated) 
    { 
      if (String.IsNullOrEmpty(View)) 
         return; 
      var result = new ViewResult {ViewName = View, MasterName = Master}; 
      filterContext.Result = result; 
   } 
 }
}

答案 1 :(得分:2)

我们可以使用here

中的Mode功能
 Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

并在任何一组功能中使用它。一个选项是dplyr

library(dplyr)
df %>%
   group_by(location) %>% 
   summarise(site = Mode(site))
#    location  site
#     <chr> <chr>
#1        a  site
#2        b  bang
#3        c  site

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)