diff --git a/src/Survey.jl b/src/Survey.jl index e617bbbf..0e2639e3 100644 --- a/src/Survey.jl +++ b/src/Survey.jl @@ -14,6 +14,7 @@ include("example.jl") include("svyglm.jl") include("svyhist.jl") include("svyplot.jl") +include("dimnames.jl") export svydesign, svyby, svyglm export data, api, apiclus1, apiclus2, apipop, apistrat, apisrs @@ -21,6 +22,7 @@ export svymean, svytotal, svyquantile export @formula export svyhist, sturges, freedman_diaconis export svyplot +export dim, colnames, dimnames export #families Normal, diff --git a/src/dimnames.jl b/src/dimnames.jl new file mode 100644 index 00000000..d508c878 --- /dev/null +++ b/src/dimnames.jl @@ -0,0 +1,72 @@ +""" + dim(design) +Get the dimensions of a survey design. + +```jldoctest +julia> using Survey + +julia> data(api); + +julia> dstrat = svydesign(data = apistrat, id = :1, strata = :stype, weights = :pw, fpc = :fpc); + +julia> dim(dstrat) +(200, 44) +``` +""" +dim(design::svydesign) = size(design.variables) + +""" + colnames(design) +Get the column names of a survey design. + +```jldoctest +julia> using Survey + +julia> data(api); + +julia> dstrat = svydesign(data = apistrat, id = :1, strata = :stype, weights = :pw, fpc = :fpc); + +julia> colnames(dstrat) +44-element Vector{String}: + "Column1" + "cds" + "stype" + "name" + "sname" + "snum" + "dname" + "dnum" + "cname" + "cnum" + ⋮ + "emer" + "enroll" + "api.stu" + "pw" + "fpc" + "probs" + "popsize" + "sampsize" + "strata" +``` +""" +colnames(design::svydesign) = names(design.variables) + +""" + dimnames(design) +Get the names of the rows and columns of a survey design. + +```jldoctest +julia> using Survey + +julia> data(api); + +julia> dstrat = svydesign(data = apistrat, id = :1, strata = :stype, weights = :pw, fpc = :fpc); + +julia> dimnames(dstrat) +2-element Vector{Vector{String}}: + ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10" … "191", "192", "193", "194", "195", "196", "197", "198", "199", "200"] + ["Column1", "cds", "stype", "name", "sname", "snum", "dname", "dnum", "cname", "cnum" … "full", "emer", "enroll", "api.stu", "pw", "fpc", "probs", "popsize", "sampsize", "strata"] +``` +""" +dimnames(design::svydesign) = [string.(1:size(design.variables, 1)), names(design.variables)] diff --git a/test/dimnames.jl b/test/dimnames.jl new file mode 100644 index 00000000..515563b3 --- /dev/null +++ b/test/dimnames.jl @@ -0,0 +1,15 @@ +using Survey +using Test + +@testset "dimnames.jl" begin + data(api) + dstrat = svydesign(data = apistrat, id = :1, strata = :stype, weights = :pw, fpc = :fpc) + + @test dim(dstrat)[1] == 200 + @test dim(dstrat)[2] == size(dstrat.variables, 2) + + @test length(colnames(dstrat)) == dim(dstrat)[2] + + @test length(dimnames(dstrat)[1]) == parse(Int, last(dimnames(dstrat)[1])) + @test dimnames(dstrat)[2] == colnames(dstrat) +end diff --git a/test/runtests.jl b/test/runtests.jl index 0cbd9b94..1f07981a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,4 +18,6 @@ end include("svyglm.jl") include("svyhist.jl") +include("svyplot.jl") +include("dimnames.jl") include("svyplot.jl")