答案 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)