Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算[R]中标称变量和连续变量的模式_R - Fatal编程技术网

计算[R]中标称变量和连续变量的模式

计算[R]中标称变量和连续变量的模式,r,R,有人能帮我吗 如果我跑步: > mode(iris$Species) [1] "numeric" > mode(iris$Sepal.Width) [1] "numeric" 然后我得到的答案是“数值” 干杯 M请参见?模式:模式将为您提供存储模式。如果需要具有最大计数的值,请使用table > Sample <- sample(letters[1:5],50,replace=T) > tmp <- table(Sample) > tmp Sample

有人能帮我吗

如果我跑步:

> mode(iris$Species)
[1] "numeric"
> mode(iris$Sepal.Width)
[1] "numeric"
然后我得到的答案是“数值”

干杯


M

请参见
?模式
模式
将为您提供存储模式。如果需要具有最大计数的值,请使用table

> Sample <- sample(letters[1:5],50,replace=T)
> tmp <- table(Sample)
> tmp
Sample
 a  b  c  d  e 
 9 12  9  7 13 
> tmp[which(tmp==max(tmp))]
 e 
13 
>示例tmp tmp
样品
a、b、c、d、e
9 12  9  7 13 
>tmp[其中(tmp==max(tmp))]
E
13
如果函数没有执行您认为应该执行的操作,请阅读帮助文件

一些额外的解释:

max(tmp)
tmp

tmp==max(tmp)
给出一个长度为tmp的逻辑向量,指示值是否等于max(tmp)

which(tmp==max(tmp))
返回向量中
TRUE
值的索引。这些索引用于选择tmp中的最大值

请参阅帮助文件
?which
?max
和R的介绍手册。

函数
mode()
用于查找对象的存储模式,在这种情况下,存储为模式
“numeric”
。此函数不用于查找数据集中最“频繁”的观测值,即不用于查找统计模式。有关此函数在R中的作用以及为什么它对您的问题没有帮助的更多信息,请参见
?模式

对于离散数据,模式是集合中最常见的观测值:

> set.seed(1) ## reproducible example
> dat <- sample(1:5, 100, replace = TRUE) ## dummy data
> (tab <- table(dat)) ## tabulate the frequencies
dat
 1  2  3  4  5 
13 25 19 26 17 
> which.max(tab) ## which is the mode?
4 
4 
> tab[which.max(tab)] ## what is the frequency of the mode?
 4 
26

在这种情况下,它不会改变结果。

这非常有效。我是R的新手,但我想了解你做了什么…你介意简要解释一下tmp[which(tmp==max(tmp))]部分吗?请记住,
which.max
只返回第一个具有最大值的索引。比较:
x
> sepalwd <- with(iris, density(Sepal.Width)) ## kernel density estimate
> plot(sepalwd)
> str(sepalwd)
List of 7
 $ x        : num [1:512] 1.63 1.64 1.64 1.65 1.65 ...
 $ y        : num [1:512] 0.000244 0.000283 0.000329 0.000379 0.000436 ...
 $ bw       : num 0.123
 $ n        : int 150
 $ call     : language density.default(x = Sepal.Width)
 $ data.name: chr "Sepal.Width"
 $ has.na   : logi FALSE
 - attr(*, "class")= chr "density"
> with(sepalwd, which.max(y)) ## which value has maximal density?
[1] 224
> with(sepalwd, x[which.max(y)]) ## use the above to find the mode
[1] 3.000314
> sepalwd2 <- with(iris, density(Sepal.Width, n = 2048))
> with(sepalwd, x[which.max(y)])
[1] 3.000314