[R] histogram frequency weighing

Sebastian P. Luque spluque at gmail.com
Mon Sep 18 00:27:57 CEST 2006


On Sun, 17 Sep 2006 15:12:30 -0500,
"Sebastian P. Luque" <spluque at gmail.com> wrote:

[...]

> I thought about some very contrived ways to accomplish this, involving
> 'which' and 'diff', but I sense a function might already be available to
> do this efficiently.

I think I found a better combination of those two functions than I what I
was initially playing with:


R> lenh <- hist(iris$Sepal.Length, br=seq(4, 8, 0.05))
R> ok <- which(lenh$counts > 0)
R> lenh$counts[ok] <- lenh$counts[ok] / diff(c(0, ok))
R> lenh$counts
 [1] 0.0000 0.0000 0.0000 0.0000 0.0000 0.1667 0.0000 1.5000 0.0000 0.5000
[11] 0.0000 2.0000 0.0000 1.0000 0.0000 2.5000 0.0000 3.0000 0.0000 5.0000
[21] 0.0000 4.5000 0.0000 2.0000 0.0000 0.5000 0.0000 3.0000 0.0000 3.5000
[31] 0.0000 3.0000 0.0000 4.0000 0.0000 3.5000 0.0000 1.5000 0.0000 3.0000
[41] 0.0000 3.0000 0.0000 2.0000 0.0000 4.5000 0.0000 3.5000 0.0000 2.5000
[51] 0.0000 1.0000 0.0000 4.0000 0.0000 1.5000 0.0000 2.0000 0.0000 0.5000
[61] 0.0000 0.5000 0.0000 1.5000 0.0000 0.5000 0.0000 0.5000 0.0000 0.0000
[71] 0.0000 0.2500 0.0000 2.0000 0.0000 0.0000 0.0000 0.2500 0.0000 0.0000


It makes sense, although I'm a bit nervous about floating-point issues
that might make this fail in some cases.  Any suggestions/comments
welcome.


-- 
Seb



More information about the R-help mailing list