NamedArrays.jl Package in Julia
Description
Julia’s NamedArrays.jl
package allows the use of 2D arrays with named rows and columns. Although it is also applicable to arrays with three or more dimensions, this article focuses on 2D arrays.
Code
Definition
When inputting an array of size $3 \times 4$ into the NamedArray
function, it outputs the array with names attached to rows and columns.
julia> using NamedArrays
julia> X = reshape(1:12, (3, 4))
3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64:
1 4 7 10
2 5 8 11
3 6 9 12
julia> A = NamedArray(X)
3×4 Named Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}
A ╲ B │ 1 2 3 4
-───┼───────-
1 │ 1 4 7 10
2 │ 2 5 8 11
3 │ 3 6 9 12
Specifying the type and entering a tuple will return a random array.
julia> NamedArray{Float64}(2,2)
2×2 Named Matrix{Float64}
A ╲ B │ 1 2
──────┼───────────────────
1 │ 0.0 1.0e-323
2 │ 5.0e-324 1.5e-323
julia> NamedArray{Int16}(2,2)
2×2 Named Matrix{Int16}
A ╲ B │ 1 2
──────┼───────────────
1 │ 30352 616
2 │ -13498 0
julia> NamedArray{Char}(2,2)
2×2 Named Matrix{Char}
A ╲ B │ 1 2
──────┼───────────────────────────────────────
1 │ '\x00\x00\x00\x9d' '\x00\x00\x00\x12'
2 │ '\0' '\0'
Naming
If you want to name the first dimension (rows), input as follows: setnames!(A, ["first row name", "second row name"...], 1)
.
julia> setnames!(A, ["1행", "2행", "3행"], 1)
(OrderedCollections.OrderedDict{Any, Int64}("1행" => 1, "2행" => 2, "3행" => 3), OrderedCollections.OrderedDict{Any, Int64}("1" => 1, "2" => 2, "3" => 3, "4" => 4))
julia> A
3×4 Named Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}
A ╲ B │ 1 2 3 4
-───┼───────-
1행 │ 1 4 7 10
2행 │ 2 5 8 11
3행 │ 3 6 9 12
julia> setnames!(A, ["가", "나", "다", "라"], 2)
(OrderedCollections.OrderedDict{Any, Int64}("1행" => 1, "2행" => 2, "3행" => 3), OrderedCollections.OrderedDict{Any, Int64}("가" => 1, "나" => 2, "다" => 3, "라" => 4))
julia> A
3×4 Named Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}
A ╲ B │ 가 나 다 라
-───┼───────-
1행 │ 1 4 7 10
2행 │ 2 5 8 11
3행 │ 3 6 9 12
Alternatively, you can specify names during definition like this:
julia> NamedArray(X, (["1행", "2행", "3행"], ["가", "나", "다", "라"]))
3×4 Named Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}
A ╲ B │ 가 나 다 라
-───┼───────-
1행 │ 1 4 7 10
2행 │ 2 5 8 11
3행 │ 3 6 9 12
julia> NamedArray(X, (["1행", "2행", "3행"], ["가", "나", "다", "라"]), ("Rows", "Cols"))
3×4 Named Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}
Rows ╲ Cols │ 가 나 다 라
-──────┼───────
1행 │ 1 4 7 10
2행 │ 2 5 8 11
3행 │ 3 6 9 12
Indexing and Slicing
Names of rows or columns can be used for indexing and slicing.
julia> A["1행", "나"]
4
julia> A["1행", 2]
4
julia> A["2행", :]
4-element Named Vector{Int64}
B │
-─┼──
가 │ 2
나 │ 5
다 │ 8
라 │ 11
julia> A[["1행", "2행", "3행"], "다"]
3-element Named Vector{Int64}
A │
──┼──
1행 │ 7
2행 │ 8
3행 │ 9
Operations
Although NamedArray
looks similar to a DataFrame, it is a subtype of AbstractArray
, which is its most distinct feature. Unlike DataFrames, it allows for operations. When adding two NamedArrays of the same size, if the row and column names do not match, the names of the left array are used as the basis.
julia> supertype(NamedArray)
AbstractArray
julia> B = NamedArray(ones(3,4), [["a", "b", "c"], ["A", "B", "C", "D"]])
3×4 Named Matrix{Float64}
A ╲ B │ A B C D
-───┼──────────
a │ 1.0 1.0 1.0 1.0
b │ 1.0 1.0 1.0 1.0
c │ 1.0 1.0 1.0 1.0
julia> C = NamedArray(2ones(3,4))
3×4 Named Matrix{Float64}
A ╲ B │ 1 2 3 4
-───┼──────────
1 │ 2.0 2.0 2.0 2.0
2 │ 2.0 2.0 2.0 2.0
3 │ 2.0 2.0 2.0 2.0
julia> B+C
┌ Warning: Using names of left argument
└ @ NamedArrays C:\Users\rydbr\.julia\packages\NamedArrays\73Upr\src\arithmetic.jl:25
3×4 Named Matrix{Float64}
A ╲ B │ A B C D
-───┼──────────
a │ 3.0 3.0 3.0 3.0
b │ 3.0 3.0 3.0 3.0
c │ 3.0 3.0 3.0 3.0
julia> C+B
┌ Warning: Using names of left argument
└ @ NamedArrays C:\Users\rydbr\.julia\packages\NamedArrays\73Upr\src\arithmetic.jl:25
3×4 Named Matrix{Float64}
A ╲ B │ 1 2 3 4
-───┼──────────
1 │ 3.0 3.0 3.0 3.0
2 │ 3.0 3.0 3.0 3.0
3 │ 3.0 3.0 3.0 3.0
For multiplication, names are assigned logically.
julia> B*C'
3×3 Named Matrix{Float64}
A ╲ A │ 1 2 3
-───┼──────-
a │ 8.0 8.0 8.0
b │ 8.0 8.0 8.0
c │ 8.0 8.0 8.0
julia> C' * B
4×4 Named Matrix{Float64}
B ╲ B │ A B C D
-───┼─────────-
1 │ 6.0 6.0 6.0 6.0
2 │ 6.0 6.0 6.0 6.0
3 │ 6.0 6.0 6.0 6.0
4 │ 6.0 6.0 6.0 6.0
Environment
- OS: Windows 11
- Version: Julia 1.10.0, NamedArrays v0.10.1