在R中,mean()和median()是标准函数,它们执行您所期望的功能。Mode()告诉您对象的内部存储模式,而不是参数中出现次数最多的值。但是是否存在一个标准库函数来实现向量(或列表)的统计模式?


当前回答

您还可以计算一个实例在您的集合中出现的次数,并找到最大次数。如。

> temp <- table(as.vector(x))
> names (temp)[temp==max(temp)]
[1] "1"
> as.data.frame(table(x))
r5050 Freq
1     0   13
2     1   15
3     2    6
> 

其他回答

这建立在jprockbelly的答案上,通过对非常短的向量增加速度。这在将mode应用到data.frame或包含很多小组的数据表时非常有用:

Mode <- function(x) {
   if ( length(x) <= 2 ) return(x[1])
   if ( anyNA(x) ) x = x[!is.na(x)]
   ux <- unique(x)
   ux[which.max(tabulate(match(x, ux)))]
}

对此有多种解决方案。我检查了第一个,然后写了我自己的。把它贴在这里,如果它能帮助到任何人:

Mode <- function(x){
  y <- data.frame(table(x))
  y[y$Freq == max(y$Freq),1]
}

让我们用几个例子来测试一下。我正在取虹膜数据集。让我们用数值数据进行测试

> Mode(iris$Sepal.Length)
[1] 5

你可以验证这是正确的。

现在虹膜数据集中唯一的非数字字段(Species)没有模式。让我们用我们自己的例子进行测试

> test <- c("red","red","green","blue","red")
> Mode(test)
[1] red

EDIT

正如注释中提到的,用户可能希望保留输入类型。在这种情况下,mode函数可以修改为:

Mode <- function(x){
  y <- data.frame(table(x))
  z <- y[y$Freq == max(y$Freq),1]
  as(as.character(z),class(x))
}

函数的最后一行只是将最终的模式值强制为原始输入的类型。

在我看来,如果一个集合有一个模式,那么它的元素就可以与自然数一一对应。因此,查找模式的问题简化为生成这样一个映射,查找映射值的模式,然后映射回集合中的一些项。(处理NA发生在映射阶段)。

我有一个直方图函数,它的原理类似。(本文代码中使用的特殊函数和操作符应在Shapiro和/或neatOveRse中定义。在此复制夏皮罗和奈尔斯的部分是经过允许的;复制的片段可根据本网站的条款使用。)直方图的伪代码是

.histogram <- function (i)
        if (i %|% is.empty) integer() else
        vapply2(i %|% max %|% seqN, `==` %<=% i %O% sum)

histogram <- function(i) i %|% rmna %|% .histogram

(特殊的二进制操作符完成管道、咖喱和组合)我还有一个maxloc函数,它与which类似。Max,但返回一个向量的所有绝对最大值。maxloc的R伪代码是

FUNloc <- function (FUN, x, na.rm=F)
        which(x == list(identity, rmna)[[na.rm %|% index.b]](x) %|% FUN)

maxloc <- FUNloc %<=% max

minloc <- FUNloc %<=% min # I'M THROWING IN minloc TO EXPLAIN WHY I MADE FUNloc

Then

imode <- histogram %O% maxloc

and

x %|% map %|% imode %|% unmap

将计算任何集合的模式,只要定义了适当的映射-ping和取消映射-ping函数。

下面的函数有三种形式:

method = "mode"[默认值]:计算单模态向量的模式,否则返回NA Method = "nmodes":计算vector中模式的个数 Method = "modes":列出单模态或多模态向量的所有模态

modeav <- function (x, method = "mode", na.rm = FALSE)
{
  x <- unlist(x)
  if (na.rm)
    x <- x[!is.na(x)]
  u <- unique(x)
  n <- length(u)
  #get frequencies of each of the unique values in the vector
  frequencies <- rep(0, n)
  for (i in seq_len(n)) {
    if (is.na(u[i])) {
      frequencies[i] <- sum(is.na(x))
    }
    else {
      frequencies[i] <- sum(x == u[i], na.rm = TRUE)
    }
  }
  #mode if a unimodal vector, else NA
  if (method == "mode" | is.na(method) | method == "")
  {return(ifelse(length(frequencies[frequencies==max(frequencies)])>1,NA,u[which.max(frequencies)]))}
  #number of modes
  if(method == "nmode" | method == "nmodes")
  {return(length(frequencies[frequencies==max(frequencies)]))}
  #list of all modes
  if (method == "modes" | method == "modevalues")
  {return(u[which(frequencies==max(frequencies), arr.ind = FALSE, useNames = FALSE)])}  
  #error trap the method
  warning("Warning: method not recognised.  Valid methods are 'mode' [default], 'nmodes' and 'modes'")
  return()
}

我发现Ken Williams上面的帖子很棒,我添加了几行来解释NA值,并使其成为一个函数。

Mode <- function(x, na.rm = FALSE) {
  if(na.rm){
    x = x[!is.na(x)]
  }

  ux <- unique(x)
  return(ux[which.max(tabulate(match(x, ux)))])
}