Skip to content

Commit

Permalink
Allow strings for column names
Browse files Browse the repository at this point in the history
Since these are allowed by DataFrames.
  • Loading branch information
nalimilan committed Oct 11, 2020
1 parent ab7abf5 commit 6b7ab47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/freqtable.jl
Expand Up @@ -87,7 +87,7 @@ end
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
freqtable(t, cols::Symbol...;
freqtable(t, cols::Union{Symbol, AbstractString}...;
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
Expand Down Expand Up @@ -191,9 +191,9 @@ freqtable(x::AbstractCategoricalVector...; skipmissing::Bool = false,
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing) =
_freqtable(x, skipmissing, weights, subset)

function freqtable(t, cols::Symbol...; args...)
function freqtable(t, cols::Union{Symbol, AbstractString}...; args...)
all_cols = Tables.columns(t)
a = freqtable((Tables.getcolumn(all_cols, y) for y in cols)...; args...)
a = freqtable((Tables.getcolumn(all_cols, Symbol(y)) for y in cols)...; args...)
setdimnames!(a, cols)
a
end
Expand Down Expand Up @@ -286,7 +286,7 @@ prop(tbl::NamedArray{<:Number}; margins=nothing) =
weights::AbstractVector{<:Real} = UnitWeights(),
subset::Union{Nothing, AbstractVector{Int}, AbstractVector{Bool}} = nothing])
proptable(t, cols::Symbol...;
proptable(t, cols::Union{Symbol, AbstractString}...;
margins = nothing,
skipmissing::Bool = false,
weights::AbstractVector{<:Real} = UnitWeights(),
Expand Down Expand Up @@ -404,5 +404,5 @@ proptable(x::AbstractVector...;
prop(freqtable(x...,
skipmissing=skipmissing, weights=weights, subset=subset), margins=margins)

proptable(t, cols::Symbol...; margins=nothing, kwargs...) =
proptable(t, cols::Union{Symbol, AbstractString}...; margins=nothing, kwargs...) =
prop(freqtable(t, cols...; kwargs...), margins=margins)
6 changes: 6 additions & 0 deletions test/freqtable.jl
Expand Up @@ -160,6 +160,9 @@ for docat in [false, true]
iris.LongSepal = iris.SepalLength .> 5.0
end
tab = freqtable(iris, :Species, :LongSepal)
@test tab == freqtable(iris, "Species", "LongSepal") ==
freqtable(iris, "Species", :LongSepal) ==
freqtable(iris, :Species, "LongSepal")
@test tab == [2 3
0 5
1 6]
Expand Down Expand Up @@ -203,6 +206,9 @@ intft = freqtable(df, :A, :B)
df = DataFrame(x = [1, 2, 1, 2], y = [1, 1, 2, 2], z = ["a", "a", "c", "d"])

tab = proptable(df, :x, :z)
@test tab == proptable(df, "x", "z") ==
proptable(df, "x", :z)
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"]]
Expand Down

0 comments on commit 6b7ab47

Please sign in to comment.