yukke::note

technical note

Rのhist()

Rで、

data <- read.table("data.txt")
hist(data, breaks=seq(1,100, 20), col='red')

f:id:soh3914:20121008144059p:plain みたいにhist()でヒストグラムをよく書く。
このとき、binのなかに含まれるデータの個数が知りたかったりする。

in_data <- hist(data)

とすると、

$breaks
 [1]   0  10  20  30  40  50  60  70  80  90 100

$counts
 [1] 65  1  2  0  1  0  1  0  0  1

$intensities
 [1] 0.091549296 0.001408451 0.002816901 0.000000000 0.001408451 0.000000000
 [7] 0.001408451 0.000000000 0.000000000 0.001408451

$density
 [1] 0.091549296 0.001408451 0.002816901 0.000000000 0.001408451 0.000000000
 [7] 0.001408451 0.000000000 0.000000000 0.001408451

$mids
 [1]  5 15 25 35 45 55 65 75 85 95

$xname
[1] "s"

$equidist
[1] TRUE

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

 って表示された。なるほど、data$countsで$breaksに入った個数だ。ただし、実行する度にプロットされてしまうし、あんまりクールではない。