Skip to content

Commit

Permalink
Remove trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed May 7, 2020
1 parent 8b3dcd5 commit 632cd98
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
54 changes: 27 additions & 27 deletions src/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ end

"""
freqtable(x::AbstractVector...;
skipmissing::Bool = false,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
freqtable(t, cols::Symbol...;
skipmissing::Bool = false,
freqtable(t, cols::Symbol...;
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
Create frequency table from vectors or table columns.
Create frequency table from vectors or table columns.
`t` can be any type of table supported by the [Tables.jl](https://github.com/JuliaData/Tables.jl) interface.
**Examples**
```jldoctest
Expand All @@ -116,14 +116,14 @@ 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
```
```
"""

freqtable(x::AbstractVector...;
Expand Down Expand Up @@ -208,7 +208,7 @@ If `margins` is an `Integer`, or an iterable of `Integer`s, 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
Expand Down Expand Up @@ -268,7 +268,7 @@ julia> sum(pt, dims=(1, 2))
function prop(tbl::AbstractArray{<:Number,N}; margins=nothing) where N
if margins === nothing
return tbl / sum(tbl)
else
else
lo, hi = extrema(margins)
(lo < 1 || hi > N) && throw(ArgumentError("margins must be a valid dimension"))
return tbl ./ sum(tbl, dims=tuple(setdiff(1:N, margins)...)::NTuple{N-length(margins),Int})
Expand All @@ -279,15 +279,15 @@ prop(tbl::NamedArray{<:Number}; margins=nothing) =
NamedArray(prop(convert(Array, tbl); margins=margins), tbl.dicts, tbl.dimnames)

"""
proptable(x::AbstractVector...;
proptable(x::AbstractVector...;
margins = nothing,
skipmissing::Bool = false,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
proptable(t, cols::Symbol...;
proptable(t, cols::Symbol...;
margins = nothing,
skipmissing::Bool = false,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
Expand Down Expand Up @@ -317,7 +317,7 @@ Dim1 │
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)
Expand Down Expand Up @@ -353,7 +353,7 @@ 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}
Expand Down Expand Up @@ -382,8 +382,8 @@ Dim1 ╲ Dim2 │ 1 2
Dim1 ╲ Dim2 │ 1 2
────────────┼─────────
1 │ 0.0 0.5
2 │ 0.0 0.5
2 │ 0.0 0.5
julia> sum(pt, dims=3)
2×2×1 Named Array{Float64,3}
Expand All @@ -392,16 +392,16 @@ Dim1 ╲ Dim2 │ 1 2
────────────┼─────────
1 │ 1.0 1.0
2 │ 1.0 1.0
```
```
"""
proptable(x::AbstractVector...;
margins = nothing,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing) =
prop(freqtable(x...,
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing) =
prop(freqtable(x...,
skipmissing=skipmissing, weights=weights, subset=subset), margins=margins)
proptable(t, cols::Symbol...; margins=nothing, kwargs...) =

proptable(t, cols::Symbol...; margins=nothing, kwargs...) =
prop(freqtable(t, cols...; kwargs...), margins=margins)
25 changes: 12 additions & 13 deletions test/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ tabc = @inferred freqtable(mcx, mcy, skipmissing=true)
using DataFrames

for docat in [false, true]
iris = DataFrame(SepalLength=[4.8, 4.3, 5.8, 5.7, 5.4, 5.7, 5.7, 6.2,
iris = DataFrame(SepalLength=[4.8, 4.3, 5.8, 5.7, 5.4, 5.7, 5.7, 6.2,
5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9],
SepalWidth=[3, 3, 4, 4.4, 3.9, 3, 2.9, 2.9, 2.5, 2.8,
SepalWidth=[3, 3, 4, 4.4, 3.9, 3, 2.9, 2.9, 2.5, 2.8,
3.3, 2.7, 3, 2.9, 3, 3, 2.5],
Species=["Iris-setosa", "Iris-setosa", "Iris-setosa",
"Iris-setosa", "Iris-setosa", "Iris-versicolor",
"Iris-versicolor", "Iris-versicolor", "Iris-versicolor",
"Iris-versicolor", "Iris-virginica", "Iris-virginica",
"Iris-virginica", "Iris-virginica", "Iris-virginica",
Species=["Iris-setosa", "Iris-setosa", "Iris-setosa",
"Iris-setosa", "Iris-setosa", "Iris-versicolor",
"Iris-versicolor", "Iris-versicolor", "Iris-versicolor",
"Iris-versicolor", "Iris-virginica", "Iris-virginica",
"Iris-virginica", "Iris-virginica", "Iris-virginica",
"Iris-virginica", "Iris-virginica"])
if docat
iris.LongSepal = categorical(iris.SepalLength .> 5.0)
Expand All @@ -174,7 +174,6 @@ for docat in [false, true]

@test_throws ArgumentError freqtable(iris)
@test_throws ArgumentError freqtable(nothing, :Species, :LongSepal)

end

# Issue #5
Expand All @@ -196,20 +195,20 @@ intft = freqtable(df, :A, :B)

@test_throws BoundsError intft[101,"x"]
@test intft[Name(101),"x"] == 1

# proptable
df = DataFrame(x = [1, 2, 1, 2], y = [1, 1, 2, 2], z = ["a", "a", "c", "d"])

tab = proptable(df, :x, :z)
tab = proptable(df, :x, :z)
@test tab == [0.25 0.25 0.0
0.25 0.0 0.25]
@test names(tab) == [[1, 2], ["a", "c", "d"]]
tab = proptable(df, :x, :z, margins=1)

tab = proptable(df, :x, :z, margins=1)
@test tab == [0.5 0.5 0.0
0.5 0.0 0.5]

tab = proptable(df, :x, :y, margins=(1,2))
tab = proptable(df, :x, :y, margins=(1,2))
@test tab == [1.0 1.0
1.0 1.0]
@test names(tab) == [[1, 2], [1, 2]]
Expand Down

0 comments on commit 632cd98

Please sign in to comment.