# Query on histc and histogram counts

**URL:** https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371
**Category:** Statistics
**Created:** [January 9, 2017, 5:03pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371 "2017-01-09T17:03:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Donald\_Lacombe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donald_lacombe/32/13500_2.png) [@Donald\_Lacombe](https://discourse.julialang.org/u/Donald_Lacombe)
#### Post date: [January 9, 2017, 5:03pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371/1 "2017-01-09T17:03:08Z")

</div>

Dear Julia Users,

I want to count the number of times a zip code appears in a vector and found the following topic:

> [@Hist() and hits() in Julia?](https://discourse.julialang.org/t/hist-and-hits-in-julia/1224/2):
>
> You can use [StatsBase](https://github.com/JuliaStats/StatsBase.jl) for this julia\> using StatsBase julia\> result = fit(Histogram, randn(1000)) StatsBase.Histogram{Int64,1,Tuple{FloatRange{Float64}}} edges: -4.0:1.0:4.0 weights: [2,24,131,361,342,129,10,1] closed: right Replace the randn(1000) in the code above with the vector your are working with. You can access the properties of the result using result.edges and result.weights. See [http://statsbasejl.readthedocs.io/en/latest/empirical.html](http://statsbasejl.readthedocs.io/en/latest/empirical.html)

I ran the following code:

```julia
using StatsBase
using DataFrames
df1 = readtable("Test2.csv")

Out[4]:
irn
1	43752
2	43752
3	43752
4	43752
5	43752
6	43752
7	43752
8	43752
9	43752
10	43752
11	43752
12	43752
13	43752
14	43752
15	43752
16	43752
17	43752
18	43752
19	43752
20	43752
21	43752
22	43752
23	43752
24	43752
25	43752
26	43752
27	43752
28	43752
29	43752
30	43752
⋮	⋮
results = fit(Histogram, df1[:,1])
StatsBase.Histogram{Int64,1,Tuple{FloatRange{Float64}}}
edges:
  43500.0:500.0:50500.0
weights: [3411,3197,882,640,16,2573,0,799,0,0,0,0,0,729]
closed: right

```

However, the above does not give me the correct counts, which I verified in MATLAB.  
I then ran the following code:

```julia
results = fit(Histogram, df1[:,1], unique(df1[:,1]))

StatsBase.Histogram{Int64,1,Tuple{DataArrays.DataArray{Int64,1}}}
edges:
  [43752,43851,44008,44081,44107,44214,44230,44271,44289,44313 … 47340,47365,47373,47381,47399,50419,50427,50435,50450,50468]
weights: [133,214,345,818,215,31,299,154,92,862 … 43,65,358,128,37,87,321,221,95,5]
closed: right

```

This code does give me the correct counts for all zip codes but the first one, i.e. 43752, which should have a count os 3278.

I’m at a loss as to why it doesn’t produce the correct counts for the first zip code.

If anyone can provide help it would be greatly appreciated.

Sincerely,  
Donald Lacombe

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [January 9, 2017, 5:09pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371/2 "2017-01-09T17:09:40Z")

</div>

Histograms are not suited/overkill for counting data: they are made for continuous variables. Use the [counting functions from StatsBase](http://statsbasejl.readthedocs.io/en/latest/counts.html) or the [FreqTables package](https://github.com/nalimilan/FreqTables.jl).

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [January 9, 2017, 5:11pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371/3 "2017-01-09T17:11:25Z")

</div>

I don’t think a histogram is what you want. Zip code is a categorical variable even though it disguises as numeric

try

```julia
using StatsBase
countmap(mynumbers)

```

for example

```julia
using StatsBase
mynumbers = [1,1,1,2,2,2,2,2,2]
countmap(mynumbers)

```

---

<div class="post-metadata">

### Author: ![Donald\_Lacombe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donald_lacombe/32/13500_2.png) [@Donald\_Lacombe](https://discourse.julialang.org/u/Donald_Lacombe)
#### Post date: [January 9, 2017, 7:33pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371/4 "2017-01-09T19:33:13Z")

</div>

Thank you for the information regarding the countmap function.

If you do not mind, I have another question.

I did the following and the results are below:

using StatsBase  
mynumbers = [1,1,1,2,2,2,2,2,2]  
result = countmap(mynumbers)

Dict{Int64,Int64} with 2 entries:  
2 =\> 6  
1 =\> 3

I then sorted the results as follows:

for key in sort(collect(keys(result)))  
println(“key =\> (result[key])”)  
end

Which resulted in the following:

1 =\> 3  
2 =\> 6

Is there a way in which I can assign the numbers 3 and 6 into a vector? I need these numbers for other calculations. I tried the following:

v = values(result)

However, I need them to be sorted in the exact order as above.

Thank you again for yor assistance.

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [January 9, 2017, 7:36pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371/5 "2017-01-09T19:36:10Z")

</div>

```julia
julia> [result[key] for key in sort(collect(keys(result)))]
2-element Array{Int64,1}:
 3
 6

```

---

<div class="post-metadata">

### Author: ![Donald\_Lacombe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donald_lacombe/32/13500_2.png) [@Donald\_Lacombe](https://discourse.julialang.org/u/Donald_Lacombe)
#### Post date: [January 9, 2017, 7:52pm UTC](https://discourse.julialang.org/t/query-on-histc-and-histogram-counts/1371/6 "2017-01-09T19:52:17Z")

</div>

I want to thank you for the quick reply to my query!

This is exactly the functionality that I was looking for and will enable me to complete the code I have been working on.

Again, thank you and all of the members of the Julia community.

Sincerely,  
Donald
