I am trying to implement an R-like matrix where the columns/rows can be referred to by named indices.
using Revise
using Base
include("nm.jl")
using .NM
nm = NM.NamedMatrix(zeros(3, 2), ["row1", "row2", "row3"], ["col1", "col2"])
nm[["row1"], ["col1"]]
where nm.jl looks like
module NM
import Base
export NamedMatrix, getindex
mutable struct NamedMatrix
matrix::Array{Float64,2}
colnames::Vector{String}
rownames::Vector{String}
end
function Base.getindex(x::NamedMatrix, i, j)
if !(i == Base.(:))
i = indexin(i, x.rownames)
end
if !(j == Base.(:))
j = indexin(j, x.colnames)
end
NamedMatrix(x.matrix[i, j],
x.rownames[i],
x.colnames[j])
end
end # module
However,
julia> nm[["row1"], ["col1"]]
gives this error:
MethodError: objects of type Module are not callable
I am not sure why this is since I don’t recognize any naming conflicts with the Module name.