# How to create and modify named vector in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412>\
**Category:** New to Julia\
**Tags:** question, arrays, namedarrays\
**Created:** [March 10, 2024, 6:41am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412 "2024-03-10T06:41:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [March 10, 2024, 6:41am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/1 "2024-03-10T06:41:57Z")

</div>

Hello,  
I am setting a polynomial function and a vector (array) that passes the parameters to this function, something like this:

```julia
y = v[1]*x + v[2]*x^2 + v[3]*x^3

```

It would be easier to remember the elements of the vector if the elements were named as in:

```julia
 y = alpha*x + beta*x^2 + gamma*x^3

```

When setting the vector, I should remember the sequence of the elements and the same applies when changing an element, as in:

```julia
julia> v = [7,2,3]
3-element Vector{Int64}:
 7
 2
 3

julia> v[2] = 5
5

julia> v
3-element Vector{Int64}:
 7
 5
 3

```

Is it possible to name each element of the vector so that I don’t need to remember the sequence of elements and that I can change the elements by name only? Something like this:

```julia
v = [beta=7, alpha=0.5, gamma=3]
v:alpha=5

```

I see from this [post](https://stackoverflow.com/questions/34561641/whats-julias-equivalent-named-vector-in-r) that one can use the package `NamedArrays` that set a vector as an array of values and names:

```julia
julia> v = NamedArray([5, 7, 3], (["alpha", "beta", "gamma"]))
3-element Named Vector{Int64}
A │ 
──────┼──
alpha │ 5
beta │ 7
gamma │ 3

```

But then how are the elements changed?  
Another way might be with the package `Parsers`:

```julia
v = (alpha=5, beta=7, gamma=3)

```

but this is a tuple that does not allow me to modify the elements.  
Is there a simple way, preferably Julia base, to do name a vector and modify its elements by name?

Thank you

---

<div class="post-metadata">

**Author:** ![sschmidhuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sschmidhuber/32/13349_2.png) [@sschmidhuber](https://discourse.julialang.org/u/sschmidhuber)\
**Post date:** [March 10, 2024, 7:24am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/2 "2024-03-10T07:24:53Z")

</div>

Hello,  
Sounds to me like what you’re looking for is a Dict (an associative array).

```julia
v = Dict(:alpha => 5, :beta => 7, :gamma => 3)
Dict{Symbol, Int64} with 3 entries:
  :alpha => 5
  :beta => 7
  :gamma => 3

julia> v[:alpha]
5

julia> v[:alpha] = 10
10

julia> v
Dict{Symbol, Int64} with 3 entries:
  :alpha => 10
  :beta => 7
  :gamma => 3

```

See [Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v1/base/collections/#Base.Dict)

It is a basic collection, so you don’t require any additional package.

BR Stefan

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 10, 2024, 7:56am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/3 "2024-03-10T07:56:09Z")

</div>

I like to use namedtuples with Accessors.jl

```julia
using Acccessors
julia> t1 = (;a=1, b=2)
(a = 1, b = 2)

julia> t2 = @set t1.a = 10
(a = 10, b = 2)

julia> t1
(a = 1, b = 2)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 10, 2024, 8:30am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/4 "2024-03-10T08:30:13Z")

</div>

See

> **[GitHub - SciML/LabelledArrays.jl: Arrays which also have a label for each...](https://github.com/SciML/LabelledArrays.jl)**
>
> Arrays which also have a label for each element for easy scientific machine learning (SciML) - SciML/LabelledArrays.jl

```julia-repl
julia> A = @LArray [1.1, 2.2, 3.3] (:alpha, :beta, :gamma)
3-element LArray{Float64, 1, Vector{Float64}, (:alpha, :beta, :gamma)}:
 :alpha => 1.1
  :beta => 2.2
 :gamma => 3.3

julia> A[1:2]
2-element Vector{Float64}:
 1.1
 2.2

julia> A.alpha
1.1

julia> A.beta
2.2

julia> A.gamma
3.3

julia> A.gamma = π
π = 3.1415926535897...

julia> A[3]
3.141592653589793

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 10, 2024, 8:48am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/5 "2024-03-10T08:48:01Z")

</div>

Is there a characteristic length to these arrays? If they are fixed for a particular problem, and reasonably short, then named tuples should be very much suited.

I suspect they are, since using individual names for fields doesn’t seem meaningful for dynamically sized collections, or large.

Tuples are lightweight and much more efficient than arrays, so `NamedTuples` seems like a good fit. Modifying them can be done efficiently by replacing the entire tuple, as @jar1 suggests.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 10, 2024, 4:52pm UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/6 "2024-03-10T16:52:25Z")

</div>

> [@Luigi\_Marongiu](#):
>
> but this is a tuple that does not allow me to modify the elements.  
> Is there a simple way, preferably Julia base, to do name a vector and modify its elements by name?

With NamedTuples and the merge function, you should be able to do all this while staying in the base

```julia
julia> v = (alpha=5, beta=7, gamma=3)
(alpha = 5, beta = 7, gamma = 3)

julia> v=merge(v,(beta=77,))
(alpha = 5, beta = 77, gamma = 3)

julia> v
(alpha = 5, beta = 77, gamma = 3)

```

---

<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:** [March 10, 2024, 6:31pm UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/7 "2024-03-10T18:31:24Z")

</div>

StaticArrays with their [`FieldVector`](https://juliaarrays.github.io/StaticArrays.jl/dev/pages/api/#FieldVector) are a great fit for such a problem: they are vectors and support vector operations, and are as performant as tuples and namedtuples.  
Of course, they are also supported by Accessors.jl for convenient value modifications.

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [March 10, 2024, 8:32pm UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/8 "2024-03-10T20:32:35Z")

</div>

If the vector has always the same number of components, you could also just create a mutable struct with those fields:

```julia
@kwdef mutable struct MyStruct
    beta::Float64
    alpha::Float64
    gamma::Float64
end

...

v = MyStruct(alpha=5, beta=7, gamma=3)
y = v.alpha * x + v.beta * x^2 + v.gamma * x^3

# Since MyStruct is mutable, you can easily change like:

v.alpha = pi
v.beta = -1
v.gamma = 0

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [March 12, 2024, 3:38pm UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/9 "2024-03-12T15:38:46Z")

</div>

Thank you but the problem with tuples is that I can’t modify them…  
I’ll try these solutions.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 12, 2024, 4:04pm UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/10 "2024-03-12T16:04:42Z")

</div>

Actually, there are two posts showing how you can ‘modify’ tuples, by creating a new one, using `merge` or `Accessors.jl`. For short tuples, this should be extremely efficient.

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [March 14, 2024, 3:33am UTC](https://discourse.julialang.org/t/how-to-create-and-modify-named-vector-in-julia/111412/11 "2024-03-14T03:33:31Z")

</div>

If you use namedtuples (or just tuples), you can also use [`evalpoly`](https://docs.julialang.org/en/v1/base/math/#Base.Math.evalpoly), which can have significant advantages over the naive way of evaluating a polynomial:

```julia
v = (beta=7, alpha=0.5, gamma=3)
evalpoly(3, Tuple(v))

```
