jar1
March 14, 2022, 9:53pm
1
In NamedArrays , is it possible to specify ("Rows", "Cols") but leaving the row and column numbers as 1,2,…?
julia> NamedArray([1 3; 2 4], ( OrderedDict("A"=>1, "B"=>2), OrderedDict("C"=>1, "D"=>2) ), ("Rows", "Cols") )
2×2 Named Matrix{Int64}
Rows ╲ Cols │ C D
────────────┼─────
A │ 1 3
B │ 2 4
julia> NamedArray([1 3; 2 4], ("Rows", "Cols") )
ERROR: MethodError: no method matching NamedArray(::Matrix{Int64}, ::Tuple{String, String})
Don’t think it’s possible, it definitely wasn’t when I tried this package some time ago.
A more flexible alternative is https://github.com/invenia/NamedDims.jl + https://github.com/mcabbott/AxisKeys.jl . The former provides named dimensions, which is exactly what you want. The latter gives custom names for rows/columns/…, and these packages are designed to work either by themselves or together.
Your example would look like NamedDimsArray([1 3; 2 4], (:Rows, :Cols)).
I tested the “NamedArrays” in this way
using NamedArrays, Plots
Plots.default(legend=false, grid=false, size=(700,300))
myArray=NamedArray(rand(-10:40, 31,12))
setnames!(myArray, ["Jan", "Feb", "Mar", "Apr", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"], 2)
show(myArray)
month="Dec"
plot(myArray[1:end,get(myArray.dicts[2],month,"Name of month is wrong")], title=month)
Your way to query the array, IMO seems more sensefull with “MetaArrays”.
Seems possible to me (in Dec 2022), so perhaps the package has been updated in the time since.
julia> a = NamedArray(rand(10,10));
julia> setdimnames!(a, [:dim1, :dim2]);
julia> a
10×10 Named Matrix{Float64}
dim1 ╲ dim2 │ 1 2 3 4 5 6 7 8 9 10
────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 0.948412 0.428683 0.902946 0.168826 0.920366 0.147852 0.174727 0.890685 0.0188128 0.650533
...
You can also do this, although not as straightforward as it could be:
a = NamedArray(rand(5,5), (1:5, 1:5), (:test1, :test2))