How to extract the frequencies from a histogram in R?

R ProgrammingServer Side ProgrammingProgramming

When we create a histogram and save it in an object name then we can extract the frequencies as count for the mid values or breaks by calling that object. We can consider that mid values or breaks obtained by the object are the actual value against which the frequencies are plotted on the histogram.

Examples

> x1<-rpois(1000,5)
> Histogram1<-hist(x1)
> Histogram1

Output

$breaks
[1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13
$counts
[1] 45 82 150 156 172 142 113 62 43 20 9 5 1
$density
[1] 0.045 0.082 0.150 0.156 0.172 0.142 0.113 0.062 0.043 0.020 0.009 0.005
[13] 0.001
$mids
[1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5
$xname
[1] "x1"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
> x2<-rpois(100000,5)
> Histogram2<-hist(x2)
> Histogram2

Output

$breaks
[1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$counts
[1] 4068 8293 14117 17627 17451 14671 10471 6604 3545 1793 810 322
[13] 152 49 12 10 4 1
$density
[1] 0.04068 0.08293 0.14117 0.17627 0.17451 0.14671 0.10471 0.06604 0.03545
[10] 0.01793 0.00810 0.00322 0.00152 0.00049 0.00012 0.00010 0.00004 0.00001
$mids
[1] 0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5 13.5 14.5
[16] 15.5 16.5 17.5
$xname
[1] "x2"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
> x3<-runif(10000,5,10)
> Histogram3<-hist(x3)
> Histogram3

Output

$breaks
[1] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
$counts
[1] 1062 1037 959 987 1010 970 942 1019 1045 969
$density
[1] 0.2124 0.2074 0.1918 0.1974 0.2020 0.1940 0.1884 0.2038 0.2090 0.1938
$mids
[1] 5.25 5.75 6.25 6.75 7.25 7.75 8.25 8.75 9.25 9.75
$xname
[1] "x3"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
> x4<-rnorm(10000,5,10)
> Histogram4<-hist(x4)
> Histogram4

Output

$breaks
[1] -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50
$counts
[1] 1 15 47 155 459 939 1458 1924 1922 1511 938 406 153 55 13
[16] 1 3
$density
[1] 0.00002 0.00030 0.00094 0.00310 0.00918 0.01878 0.02916 0.03848 0.03844
[10] 0.03022 0.01876 0.00812 0.00306 0.00110 0.00026 0.00002 0.00006
$mids
[1] -32.5 -27.5 -22.5 -17.5 -12.5 -7.5 -2.5 2.5 7.5 12.5 17.5 22.5
[13] 27.5 32.5 37.5 42.5 47.5
$xname
[1] "x4"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
raja
Published on 04-Sep-2020 12:25:25
Advertisements