Reputation: 9061
In R
, we could give names to each items in a vector:
> x = c( 2.718 , 3.14 , 1.414 , 47405 ) # define the vector
> names(x) = c( "e" , "pi" , "sqrt2" , "zipcode" ) # name the components
> x[c(2,4)] # which indices to include
pi zipcode
3.14 47405.00
> x[c(-1,-3)] # which indices to exclude
pi zipcode
3.14 47405.00
> x[c(FALSE,TRUE,FALSE,TRUE)] # for each position, include it?
pi zipcode
3.14 47405.00
In Julia
, my first thought is using a Dict
:
julia> x = [2.718, 3.14, 1.414, 47405]
4-element Array{Float64,1}:
2.718
3.14
1.414
47405.0
julia> namelist = ["e", "pi", "sqrt2", "zipcode"]
4-element Array{ASCIIString,1}:
"e"
"pi"
"sqrt2"
"zipcode"
julia> xdict=Dict(zip(namelist, x))
Dict{ASCIIString,Float64} with 4 entries:
"e" => 2.718
"zipcode" => 47405.0
"pi" => 3.14
"sqrt2" => 1.414
julia> xdict["pi"]
3.14
However, Dict
lost the order of the original array, meaning I cannot access items like in R
:
julia> xdict[[2,4]]
ERROR: KeyError: [2,4] not found in getindex at dict.jl:718
Is there something like named array in Julia
? If not, what's the Julia-way to handle this kind of problem?
Upvotes: 5
Views: 2378
Reputation: 190
Julia has the named tuple, which is a similar data structure that you can create like this:
(e=2.718, pi=3.14159, sqrt2 = 1.44, zipcode=47405)
(Do note that unlike named lists, named tuples are immutable. This leads to better performance, but means named tuples can be a bit of a pain to work with unless you use BangBang.jl, a package that lets you work with immutable objects as if they were mutable.)
Upvotes: 3
Reputation: 18227
Trying to replicate the R code using NamedArrays
:
julia> using NamedArrays
julia> xx = NamedArray([2.718, 3.14, 1.414, 47405],
(["e", "pi", "sqrt2", "zipcode"],))
4-element NamedArrays.NamedArray...
e 2.718
pi 3.14
sqrt2 1.414
zipcode 47405.0
julia> xx[[2,4]]
2-element NamedArrays.NamedArray...
pi 3.14
zipcode 47405.0
julia> xx[setdiff(1:end,[1,3])]
2-element NamedArrays.NamedArray...
pi 3.14
zipcode 47405.0
julia> xx[[1:end...][[false,true,false,true]]]
2-element NamedArrays.NamedArray...
pi 3.14
zipcode 47405.0
The indexing techniques are less than optimal. Improvements are welcome in the comments. It is also possible to enhance NamedArrays
easily for better indexing (which would be harder in R).
Upvotes: 6