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 9396cb4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 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
43 changes: 23 additions & 20 deletions test/freqtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ 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]

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

tab =freqtable(x, y,
subset=1:20,
Expand All @@ -50,13 +53,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

0 comments on commit 9396cb4

Please sign in to comment.