如何找到统计模式? [英] How to find the statistical mode?

查看:50
本文介绍了如何找到统计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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相同的方法,并返回模式集的first-appearing值.要返回 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)]
}

这篇关于如何找到统计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆