Skip to content

Commit

Permalink
refactor arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Dec 19, 2017
1 parent 961525e commit 4d64637
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
12 changes: 9 additions & 3 deletions src/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ function freqtable(d::AbstractDataFrame, x::Symbol...; args...)
a
end

function proptable(x...; dims=nothing, args...)
function proptable(x...; args...)
n = length(x)
n == 0 && throw(ArgumentError("at least one argument must be provided"))
if n > 1 && isa(x[end], Union{Integer, Tuple})
dims = x[end]
tbl = freqtable(x[1:(end-1)]...; args...)
return tbl ./ sum(tbl, dims)
end
tbl = freqtable(x...; args...)
isa(dims, Void) && return tbl / sum(tbl)
tbl ./ sum(tbl, dims)
tbl / sum(tbl)
end
45 changes: 24 additions & 21 deletions test/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ tab = @inferred freqtable(x, y)
0.075 0.05 0.05 0.075;
0.05 0.075 0.075 0.05;
0.05 0.075 0.075 0.05]
@test proptable(x, y, dims=(1,2)) == [0.075 0.05 0.05 0.075;
0.075 0.05 0.05 0.075;
0.05 0.075 0.075 0.05;
0.05 0.075 0.075 0.05]
@test proptable(x, y, dims=1) == [0.3 0.2 0.2 0.3;
0.3 0.2 0.2 0.3;
0.2 0.3 0.3 0.2;
0.2 0.3 0.3 0.2]
@test proptable(x, y, dims=2) == [0.3 0.2 0.2 0.3;
0.3 0.2 0.2 0.3;
0.2 0.3 0.3 0.2;
0.2 0.3 0.3 0.2]
@test proptable(x, y, dims=()) == [1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0]

@test proptable(x, y, (1,2)) == [0.075 0.05 0.05 0.075;
0.075 0.05 0.05 0.075;
0.05 0.075 0.075 0.05;
0.05 0.075 0.075 0.05]
@test proptable(x, y, 1) == [0.3 0.2 0.2 0.3;
0.3 0.2 0.2 0.3;
0.2 0.3 0.3 0.2;
0.2 0.3 0.3 0.2]
@test proptable(x, y, 2) == [0.3 0.2 0.2 0.3;
0.3 0.2 0.2 0.3;
0.2 0.3 0.3 0.2;
0.2 0.3 0.3 0.2]
@test proptable(x, y, ()) == [1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0;
1.0 1.0 1.0 1.0]

tab =freqtable(x, y,
subset=1:20,
Expand All @@ -50,13 +49,13 @@ tab =freqtable(x, y,
@test names(tab) == [["a", "b", "c", "d"], ["C", "D"]]
@test proptable(x, y, subset=1:20, weights=repeat([1, .5],
outer=[10])) == [4 6; 2 3; 6 4; 3 2] / 30.0
@test proptable(x, y, dims=1, subset=1:20, weights=repeat([1, .5],
@test proptable(x, y, 1, subset=1:20, weights=repeat([1, .5],
outer=[10])) == [8 12; 4 6; 12 8; 6 4] / 30.0
@test proptable(x, y, dims=2, subset=1:20, weights=repeat([1, .5],
@test proptable(x, y, 2, subset=1:20, weights=repeat([1, .5],
outer=[10])) == [6 9; 6 9; 9 6; 9 6] / 15.0
@test proptable(x, y, dims=(), subset=1:20, weights=repeat([1, .5],
@test proptable(x, y, (), subset=1:20, weights=repeat([1, .5],
outer=[10])) == [1.0 1.0; 1.0 1.0; 1.0 1.0; 1.0 1.0]
@test proptable(x, y, subset=1:20, dims=(1,2), weights=repeat([1, .5],
@test proptable(x, y, (1, 2), subset=1:20, weights=repeat([1, .5],
outer=[10])) == [4 6; 2 3; 6 4; 3 2] / 30.0

using CategoricalArrays
Expand Down Expand Up @@ -138,3 +137,7 @@ tab = freqtable(iris, :Species, :LongSepal, subset=iris[:PetalLength] .< 4.0)
# Issue #5
@test freqtable([Set(1), Set(2)]) == [1, 1]
@test freqtable([Set(1), Set(2)], [Set(1), Set(2)]) == eye(2)

@test_throws ArgumentError proptable([1,2,3], ("a","b"))
@test_throws MethodError proptable(("a","b"))
@test_throws MethodError proptable((1, 2))

0 comments on commit 4d64637

Please sign in to comment.