# NamedArray specifying only dimension names

**URL:** https://discourse.julialang.org/t/namedarray-specifying-only-dimension-names/77895
**Category:** General Usage
**Created:** [March 14, 2022, 9:53pm UTC](https://discourse.julialang.org/t/namedarray-specifying-only-dimension-names/77895 "2022-03-14T21:53:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [March 14, 2022, 9:53pm UTC](https://discourse.julialang.org/t/namedarray-specifying-only-dimension-names/77895/1 "2022-03-14T21:53:21Z")

</div>

In [NamedArrays](https://github.com/davidavdav/NamedArrays.jl#lower-level-constructors), is it possible to specify `("Rows", "Cols")` but leaving the row and column numbers as 1,2,…?

```julia
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})

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [March 15, 2022, 6:03am UTC](https://discourse.julialang.org/t/namedarray-specifying-only-dimension-names/77895/2 "2022-03-15T06:03:15Z")

</div>

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/invenia/NamedDims.jl) + [https://github.com/mcabbott/AxisKeys.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))`.

---

<div class="post-metadata">

### Author: ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)
#### Post date: [March 15, 2022, 6:41am UTC](https://discourse.julialang.org/t/namedarray-specifying-only-dimension-names/77895/3 "2022-03-15T06:41:14Z")

</div>

I tested the “NamedArrays” in this way

```julia
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”.

---

<div class="post-metadata">

### Author: ![ConnectedSystems](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/connectedsystems/32/16815_2.png) [@ConnectedSystems](https://discourse.julialang.org/u/ConnectedSystems)
#### Post date: [December 23, 2022, 6:16am UTC](https://discourse.julialang.org/t/namedarray-specifying-only-dimension-names/77895/4 "2022-12-23T06:16:28Z")

</div>

Seems possible to me (in Dec 2022), so perhaps the package has been updated in the time since.

```julia
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:

```julia
a = NamedArray(rand(5,5), (1:5, 1:5), (:test1, :test2))

```
