Skip to content

Commit

Permalink
Merge pull request #23 from iuliadmtru/dimnames
Browse files Browse the repository at this point in the history
Implement `dimnames`, `dim` and `colnames`
  • Loading branch information
ayushpatnaikgit authored Jul 30, 2022
2 parents 64143a7 + 03b77f2 commit e2138f4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Survey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ 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
export svymean, svytotal, svyquantile
export @formula
export svyhist, sturges, freedman_diaconis
export svyplot
export dim, colnames, dimnames
export
#families
Normal,
Expand Down
72 changes: 72 additions & 0 deletions src/dimnames.jl
Original file line number Diff line number Diff line change
@@ -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)]
15 changes: 15 additions & 0 deletions test/dimnames.jl
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ end

include("svyglm.jl")
include("svyhist.jl")
include("svyplot.jl")
include("dimnames.jl")
include("svyplot.jl")

0 comments on commit e2138f4

Please sign in to comment.