[R] histogram frequency weighing

Sebastian P. Luque spluque at gmail.com
Sun Sep 17 22:12:30 CEST 2006


Fellow R-helpers,

Suppose we create a histogram as follows (although it could be any vector
with zeroes in it):


R> lenh <- hist(iris$Sepal.Length, br=seq(4, 8, 0.05))
R> lenh$counts
 [1]  0  0  0  0  0  1  0  3  0  1  0  4  0  2  0  5  0  6  0 10  0  9  0  4  0
[26]  1  0  6  0  7  0  6  0  8  0  7  0  3  0  6  0  6  0  4  0  9  0  7  0  5
[51]  0  2  0  8  0  3  0  4  0  1  0  1  0  3  0  1  0  1  0  0  0  1  0  4  0
[76]  0  0  1  0  0


and we wanted to apply a weighing scheme where frequencies immediately
following (and only those) empty class intervals (0) should be adjusted by
averaging them over the number of preceding empty intervals + 1.  For
example, the first frequency that would need to be adjusted in 'lenh' is
element 6 (1), which has 5 preceding empty intervals, so its adjusted
count would be 1/6.  Similarly, the second one would be element 8 (3),
which has 1 preceding empty interval, so its adjusted count would be 3/2.
Can somebody please provide a hint to implement such a weighing scheme?

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 couldn't find relevant info in the usual channels.
Thanks in advance for any pointers.


Cheers,

-- 
Seb



More information about the R-help mailing list