Hello everyone, I am trying to figure out how to concatenate NamedArrays while maintaining column names.
using NamedArrays
countriesq = ["USq", "CNq", "JPq", "FRq", "BDq", "ITq", "UKq"]
countriesr = ["USr", "CNr", "JPr", "FRr", "BDr", "ITr", "UKr"]
q = NamedArray(zeros(138, 7))
r = NamedArray(zeros(138, 7))
setnames!(q, countriesq, 2)
setnames!(r, countriesr, 2)
If I do
z = hcat(r, q)
I get numbers 1:14 as column names instead of countriesq and countriesr names.
Thank you very much for your help.
I’m not sure what problem’s causing this, or if NamedArrays.jl just doesn’t provide this feature (concatenating while preserving names), but DimensionalData.jl shouldn’t have this same problem.
1 Like
NamedArrays.jl
don’t support keeping keys when concatenating: Keeping names in *cat · Issue #74 · davidavdav/NamedArrays.jl · GitHub. The issue is related to potentially nonunique keys after the operation.
There are multiple keyed/named array implementations that support this though.
For example, AxisKeys.jl
do allow duplicate keys and arbitrary concatenation:
julia> using AxisKeys
julia> q = KeyedArray(rand(3, 2), (["USq", "CNq", "JPq"], 1:2))
2-dimensional KeyedArray(...) with keys:
↓ 3-element Vector{String}
→ 2-element UnitRange{Int64}
And data, 3×2 Matrix{Float64}:
(1) (2)
("USq") 0.330996 0.455291
("CNq") 0.914666 0.335489
("JPq") 0.178773 0.990683
julia> r = KeyedArray(rand(3, 2), (["USq", "CNq", "JPq"], 1:2))
2-dimensional KeyedArray(...) with keys:
↓ 3-element Vector{String}
→ 2-element UnitRange{Int64}
And data, 3×2 Matrix{Float64}:
(1) (2)
("USq") 0.600243 0.997009
("CNq") 0.887196 0.979967
("JPq") 0.171013 0.663274
julia> hcat(r, q)
2-dimensional KeyedArray(...) with keys:
↓ 3-element Vector{String}
→ 4-element Vector{Int64}
And data, 3×4 Matrix{Float64}:
(1) (2) (1) (2)
("USq") 0.600243 0.997009 0.330996 0.455291
("CNq") 0.887196 0.979967 0.914666 0.335489
("JPq") 0.171013 0.663274 0.178773 0.990683
1 Like