[R] hist returning density larger than 1

Sarah Goslee sarah.goslee at gmail.com
Thu Jun 4 17:33:25 CEST 2009


Hi,

If I understand your problem correctly, you didn't need to send us
your entire code.
A simple reproducible example that showed just your problem would have been
sufficient.

Take a look at this, and reread the help for hist().

> testdata <- runif(1000)
> testdata.hist <- hist(testdata)
> testdata.hist$density
 [1] 1.190000 1.010000 0.890000 0.790000 0.920000 1.040000 1.130000 0.930000
 [9] 1.020000 1.080000


# See the help for the actual definition of density (admittedly not as clear as
# one might want).
> sum(testdata.hist$density * diff(testdata.hist$breaks))
[1] 1

# What you appear to actually want:
> testdata.hist$counts/1000
 [1] 0.119 0.101 0.089 0.079 0.092 0.104 0.113 0.093 0.102 0.108

Sarah

On Thu, Jun 4, 2009 at 11:23 AM, Steven Matthew Anderson
<adastra69 at mac.com> wrote:
> The following code is giving me problems.  I want to export densities of a
> distribution to a csv file.  At the bottom of the code I use the hist
> function to generate the densities.  But hist is returning values greater
> than 1.  I don't understand, why.  Any help you can supply is greatly
> appreciated.
>
-- 
Sarah Goslee
http://www.functionaldiversity.org




More information about the R-help mailing list