merchmallow
merchmallow

Reputation: 794

Frequencies in a vector or list using counts()

How can i use counts() to show the frequencies and the items? for example:

a=[1,2,2,3]
count(a) gives 1,2,1

How can i do to get: 1:1, 2:2, 3:1?

Thanks

Upvotes: 1

Views: 1225

Answers (2)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69939

If you prefer tabular output you can also do:

julia> using FreqTables

julia> a = [1,2,2,3];

julia> freqtable(a)
3-element Named Vector{Int64}
Dim1  │ 
──────┼──
1     │ 1
2     │ 2
3     │ 1

Upvotes: 3

PaSTE
PaSTE

Reputation: 4558

It looks like you are already using StatsBase, because that is where the counts function you mention is defined. The function you are looking for is called countmap:

using StatsBase
a = [1,2,2,3];
countmap(a)
# Dict{Int64, Int64} with 3 entries:
#   2 => 2
#   3 => 1
#   1 => 1

Upvotes: 4

Related Questions