在R中,mean()和median()是标准函数,它们执行您所期望的功能。Mode()告诉您对象的内部存储模式,而不是参数中出现次数最多的值。但是是否存在一个标准库函数来实现向量(或列表)的统计模式?
当前回答
添加raster::modal()作为一个选项,不过请注意,raster是一个很大的包,如果不做地理空间方面的工作,可能不值得安装。
源代码可以从https://github.com/rspatial/raster/blob/master/src/modal.cpp和https://github.com/rspatial/raster/blob/master/R/modal.R中取出,放入个人R包中,供那些特别热衷的人使用。
其他回答
这是我的数据。返回完整表的逐行模式的表解决方案。我用它来推断行类。它负责data中新的set()函数。桌子,应该很快。虽然它不管理NA,但可以通过查看本页上的众多其他解决方案添加。
majorityVote <- function(mat_classes) {
#mat_classes = dt.pour.centroids_num
dt.modes <- data.table(mode = integer(nrow(mat_classes)))
for (i in 1:nrow(mat_classes)) {
cur.row <- mat_classes[i]
cur.mode <- which.max(table(t(cur.row)))
set(dt.modes, i=i, j="mode", value = cur.mode)
}
return(dt.modes)
}
可能的用法:
newClass <- majorityVote(my.dt) # just a new vector with all the modes
您还可以计算一个实例在您的集合中出现的次数,并找到最大次数。如。
> 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
>
下面的函数有三种形式:
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()
}
在r邮件列表中发现了这个,希望对你有帮助。我也是这么想的。您将希望table()数据,排序,然后选择第一个名称。这有点粗俗,但应该有用。
names(sort(-table(x)))[1]
模式并不是在所有情况下都有用。所以函数应该处理这种情况。试试下面的函数。
Mode <- function(v) {
# checking unique numbers in the input
uniqv <- unique(v)
# frquency of most occured value in the input data
m1 <- max(tabulate(match(v, uniqv)))
n <- length(tabulate(match(v, uniqv)))
# if all elements are same
same_val_check <- all(diff(v) == 0)
if(same_val_check == F){
# frquency of second most occured value in the input data
m2 <- sort(tabulate(match(v, uniqv)),partial=n-1)[n-1]
if (m1 != m2) {
# Returning the most repeated value
mode <- uniqv[which.max(tabulate(match(v, uniqv)))]
} else{
mode <- "Two or more values have same frequency. So mode can't be calculated."
}
} else {
# if all elements are same
mode <- unique(v)
}
return(mode)
}
输出,
x1 <- c(1,2,3,3,3,4,5)
Mode(x1)
# [1] 3
x2 <- c(1,2,3,4,5)
Mode(x2)
# [1] "Two or more varibles have same frequency. So mode can't be calculated."
x3 <- c(1,1,2,3,3,4,5)
Mode(x3)
# [1] "Two or more values have same frequency. So mode can't be calculated."