FreqTables.freqtableMethod
freqtable(x::AbstractVector...;
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])

freqtable(t, cols::Union{Symbol, AbstractString}...;
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])

Create frequency table from vectors or table columns.

t can be any type of table supported by the Tables.jl interface.

Examples

julia> freqtable([1, 2, 2, 3, 4, 3])
4-element Named Array{Int64,1}
Dim1  │
──────┼──
1     │ 1
2     │ 2
3     │ 2
4     │ 1

julia> df = DataFrame(x=[1, 2, 2, 2], y=[1, 2, 1, 2]);

julia> freqtable(df, :x, :y)
2×2 Named Array{Int64,2}
x ╲ y │ 1  2
──────┼─────
1     │ 1  0
2     │ 1  2

julia> freqtable(df, :x, :y, subset=df.x .> 1)
1×2 Named Array{Int64,2}
x ╲ y │ 1  2
──────┼─────
2     │ 1  2

FreqTables.propMethod
prop(tbl::AbstractArray{<:Number};
margins = nothing)

Create a table of proportions from an array tbl.

If margins is nothing (the default), proportions over the whole tbl are computed. If margins is an Integer, or an iterable of Integers, proportions sum to 1 over dimensions specified by margins. In particular for a two-dimensional array, when margins is 1 row proportions are calculated, and when margins is 2 column proportions are calculated.

This function does not check that tbl contains only non-negative values.

Calculating sum(proptable(..., margins=margins), dims=dims) with dims equal to the complement of margins produces an array containing only 1.0 (see last example below).

Examples

julia> prop([1 2; 3 4])
2×2 Array{Float64,2}:
0.1  0.2
0.3  0.4

julia> prop([1 2; 3 4], margins=1)
2×2 Array{Float64,2}:
0.333333  0.666667
0.428571  0.571429

julia> prop([1 2; 3 4], margins=2)
2×2 Array{Float64,2}:
0.25  0.333333
0.75  0.666667

julia> prop([1 2; 3 4], margins=(1, 2))
2×2 Array{Float64,2}:
1.0  1.0
1.0  1.0

julia> pt = prop(reshape(1:12, (2, 2, 3)), margins=3)
2×2×3 Array{Float64,3}:
[:, :, 1] =
0.1  0.3
0.2  0.4

[:, :, 2] =
0.192308  0.269231
0.230769  0.307692

[:, :, 3] =
0.214286  0.261905
0.238095  0.285714

julia> sum(pt, dims=(1, 2))
1×1×3 Array{Float64,3}:
[:, :, 1] =
1.0

[:, :, 2] =
1.0

[:, :, 3] =
1.0

FreqTables.proptableMethod
proptable(x::AbstractVector...;
margins = nothing,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])

proptable(t, cols::Union{Symbol, AbstractString}...;
margins = nothing,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])

Create a frequency table of proportions from vectors or table columns. This is equivalent to calling prop(freqtable(...), margins=margins).

t can be any type of table supported by the Tables.jl interface.

If margins is nothing (the default), proportions over the whole table are computed. If margins is an Integer, or an iterable of Integers, proportions sum to 1 over dimensions specified by margins. In particular for a two-dimensional array, when margins is 1 row proportions are calculated, and when margins is 2 column proportions are calculated.

Calculating sum(proptable(..., margins=margins), dims=dims) with dims equal to the complement of margins produces an array containing only 1.0 (see last example below).

Examples

julia> proptable([1, 2, 2, 3, 4, 3])
4-element Named Array{Float64,1}
Dim1  │
──────┼─────────
1     │ 0.166667
2     │ 0.333333
3     │ 0.333333
4     │ 0.166667

julia> df = DataFrame(x=[1, 2, 2, 2, 1, 1], y=[1, 2, 1, 2, 2, 2], z=[1, 1, 1, 2, 2, 1]);

julia> proptable(df, :x, :y)
2×2 Named Array{Float64,2}
x ╲ y │        1         2
──────┼───────────────────
1     │ 0.166667  0.333333
2     │ 0.166667  0.333333

julia> proptable(df, :x, :y, subset=df.x .> 1)
1×2 Named Array{Float64,2}
x ╲ y │        1         2
──────┼───────────────────
2     │ 0.333333  0.666667

julia> proptable([1, 2, 2, 2], [1, 1, 1, 2], margins=1)
2×2 Named Array{Float64,2}
Dim1 ╲ Dim2 │        1         2
────────────┼───────────────────
1           │      1.0       0.0
2           │ 0.666667  0.333333

julia> proptable([1, 2, 2, 2], [1, 1, 1, 2], margins=2)
2×2 Named Array{Float64,2}
Dim1 ╲ Dim2 │        1         2
────────────┼───────────────────
1           │ 0.333333       0.0
2           │ 0.666667       1.0

julia> proptable([1, 2, 2, 2], [1, 1, 1, 2], margins=(1,2))
2×2 Named Array{Float64,2}
Dim1 ╲ Dim2 │   1    2
────────────┼─────────
1           │ 1.0  NaN
2           │ 1.0  1.0

julia> proptable(df.x, df.y, df.z)
2×2×2 Named Array{Float64,3}

[:, :, Dim3=1] =
Dim1 ╲ Dim2 │        1         2
────────────┼───────────────────
1           │ 0.166667  0.166667
2           │ 0.166667  0.166667

[:, :, Dim3=2] =
Dim1 ╲ Dim2 │        1         2
────────────┼───────────────────
1           │      0.0  0.166667
2           │      0.0  0.166667

julia> pt = proptable(df.x, df.y, df.z, margins=(1,2))
2×2×2 Named Array{Float64,3}

[:, :, Dim3=1] =
Dim1 ╲ Dim2 │   1    2
────────────┼─────────
1           │ 1.0  0.5
2           │ 1.0  0.5

[:, :, Dim3=2] =
Dim1 ╲ Dim2 │   1    2
────────────┼─────────
1           │ 0.0  0.5
2           │ 0.0  0.5

julia> sum(pt, dims=3)
2×2×1 Named Array{Float64,3}

[:, :, Dim3=sum(Dim3)] =
Dim1 ╲ Dim2 │   1    2
────────────┼─────────
1           │ 1.0  1.0
2           │ 1.0  1.0