本文介绍了是否有用于查找模式的内置功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,mean()median()是满足您期望的标准函数. mode()告诉您对象的内部存储模式,而不是在其参数中出现最多的值.但是,是否有一个标准库函数可以实现矢量(或列表)的统计模式?

In R, mean() and median() are standard functions which do what you'd expect. mode() tells you the internal storage mode of the object, not the value that occurs the most in its argument. But is there is a standard library function that implements the statistical mode for a vector (or list)?

推荐答案

另一种解决方案,对数字&字符/因素数据:

One more solution, which works for both numeric & character/factor data:

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

在我的小型机器上,该机器可以生成&在大约半秒钟内即可找到10M整数矢量的模式.

On my dinky little machine, that can generate & find the mode of a 10M-integer vector in about half a second.

如果您的数据集可能具有多种模式,则上述解决方案采用与which.max相同的方法,并返回模式集的首次出现值.要返回 all 模式,请使用以下变体(在注释中来自@digEmAll):

If your data set might have multiple modes, the above solution takes the same approach as which.max, and returns the first-appearing value of the set of modes. To return all modes, use this variant (from @digEmAll in the comments):

Modes <- function(x) {
  ux <- unique(x)
  tab <- tabulate(match(x, ux))
  ux[tab == max(tab)]
}

这篇关于是否有用于查找模式的内置功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:04