# Matrix Column/Row Labelling

**URL:** <https://discourse.julialang.org/t/matrix-column-row-labelling/84064>\
**Category:** General Usage\
**Tags:** dataframes, matrices\
**Created:** [July 11, 2022, 5:21pm UTC](https://discourse.julialang.org/t/matrix-column-row-labelling/84064 "2022-07-11T17:21:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xztt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xztt/32/33843_2.png) [@xztt](https://discourse.julialang.org/u/xztt)\
**Post date:** [July 11, 2022, 5:21pm UTC](https://discourse.julialang.org/t/matrix-column-row-labelling/84064/1 "2022-07-11T17:21:16Z")

</div>

Hi,

Is there a simple way of assigning labels to matrix columns such that if operations are performed to that matrix you still can see where that specific column has ended up for example reduced row echelon or if I performed a ordering algorithm etc.

Given a 3x3 that has the form below

```julia
 1 2 3
 4 5 6
 7 8 9

```

I want to assign labels such that col1=a, col2 = b and col3=c.

So if I called `a` it would return the result

```julia
1
4
7

```

Then if I wrote an algorithm such that it returned which columns are the largest subject to their column 1-norm in this case I would want to the algorithm to return ` c b a`.

Just to clarify not after an algorithm just the column labeling part.

Hopefully that examples clears up what I am looking for 🙂

Cheers,  
Harry

---

<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:** [July 11, 2022, 7:38pm UTC](https://discourse.julialang.org/t/matrix-column-row-labelling/84064/2 "2022-07-11T19:38:14Z")

</div>

Array indices can only be integers. However, you can attach labels/keys using a keyed array package:

```julia
julia> using AxisKeys

julia> A = KeyedArray([1 2; 3 4], x=[:a, :b], y=[:c, :d])
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓ x ∈ 2-element Vector{Symbol}
→ y ∈ 2-element Vector{Symbol}
And data, 2×2 Matrix{Int64}:
        (:c) (:d)
  (:a) 1 2
  (:b) 3 4

# regular index of the largest column
julia> argmax(sum(abs, c) for c in eachcol(A))
2

# label of the largest column:
julia> axiskeys(A, :y)[argmax(sum(abs, c) for c in eachcol(A))]
:d

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 11, 2022, 10:01pm UTC](https://discourse.julialang.org/t/matrix-column-row-labelling/84064/3 "2022-07-11T22:01:09Z")

</div>

Using DataFrames for column labelling:

```julia
using DataFrames
m = [1 2 3; 4 5 6; 7 8 9]
df = DataFrame(m, [:a, :b, :c])
df.a
names(df, sortperm(map(c->sum(abs, c), eachcol(df)), rev=true))

```

Or using DataFrames and named tuples:

```julia
nrm1 = map(c->sum(abs, c), Tables.columntable(df))
using TupleTools # to be able to sort tuples
getproperty.((keys(nrm1),), TupleTools.sortperm(values(nrm1), rev=true))

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 11, 2022, 10:17pm UTC](https://discourse.julialang.org/t/matrix-column-row-labelling/84064/4 "2022-07-11T22:17:31Z")

</div>

You have two ways. Either use the DataFrame package. That’s a mature and large package that, aside providing a named cols api, allow to efficiently work on heterogeneous yabular data (e.g. one column is an integer, another a string,…).  
While dataframes do support the so called “Table” API, they sre not AbstractArrays.  
If you want a ligher package that desls only with labeled indexes keeping an AbstractArray behaviour, there sre tens of pachages, like the mentioned KeiedArrays pachage… there sre several threads on this in the forun…
