获取R中直方图的bin宽度

Getting bin width of a histogram in R

我做了一个直方图如下:

data(mtcars)
hist(mtcars$disp)

如何找出上面直方图中的 bin 宽度?

此致

保存 hist() 调用的结果,然后提取中断:

h <- hist(mtcars$disp)

h$breaks
#>  [1]  50 100 150 200 250 300 350 400 450 500
# To get the widths, use diff().  Here all are the same:
unique(diff(h$breaks))
#> [1] 50

reprex package (v2.0.0)

于 2021-09-06 创建

感谢@BenBolker 明确计算宽度的建议。

R 在创建直方图时默认会做很多事情

您可以打印出直方图的这个参数 分配给一个对象 histinfo 然后打印:

breaks 将为您提供 bin 宽度:

histinfo<-hist(mtcars$disp)
histinfo

> histinfo
$breaks
 [1]  50 100 150 200 250 300 350 400 450 500

$counts
[1] 5 7 4 1 4 4 4 1 2

$density
[1] 0.003125 0.004375 0.002500 0.000625 0.002500 0.002500 0.002500
[8] 0.000625 0.001250

$mids
[1]  75 125 175 225 275 325 375 425 475

$xname
[1] "mtcars$disp"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"