# How to programatically construct a ComponentArray or NamedTuple?

**URL:** https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937
**Category:** General Usage
**Tags:** namedtuple, componentarrays
**Created:** [November 4, 2021, 7:36am UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937 "2021-11-04T07:36:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 4, 2021, 7:36am UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937/1 "2021-11-04T07:36:19Z")

</div>

As the title says, how can I programatically construct a `ComponentArray` or `NamedTuple` from a vector of strings (or symbols) and a vector of numbers?  
from this

```julia
n = ["a", "b", "c"]
x = [1, 2, 3]

```

I want to get this

```julia
ca = ComponentArray(a = 1, b = 2, c = 3)

```

or this

```julia
nt = (a = 1, b = 2, c = 3)

```

The application I have in mind is to construct a `ComponentArray` or `NamedTuple` from a `DataFrame`:

```julia
df = DataFrame(a = 1, b = 2, c = 3)

```

(I think the way for constructing either `ComponentArrays` or `NamedTuples` would be the same)

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [November 4, 2021, 8:46am UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937/2 "2021-11-04T08:46:34Z")

</div>

This works:

```julia
julia> (;Pair.(Symbol.(n),x)...)
(a = 1, b = 2, c = 3)

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 4, 2021, 9:27am UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937/3 "2021-11-04T09:27:13Z")

</div>

Another possibility:

```julia
NamedTuple(Symbol.(n) .=> x)

```

---

<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: [November 4, 2021, 10:09am UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937/4 "2021-11-04T10:09:49Z")

</div>

Convert any table (eg DataFrame) to a namedtuple-of-arrays: `Tables.columntable(...)`; to an array-of-namedtuples: `Tables.rowtable(...)`. These may be more helpful for your specific usecase.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [November 4, 2021, 10:54am UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937/5 "2021-11-04T10:54:04Z")

</div>

This is fantastic!

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [November 11, 2021, 3:22pm UTC](https://discourse.julialang.org/t/how-to-programatically-construct-a-componentarray-or-namedtuple/70937/6 "2021-11-11T15:22:48Z")

</div>

Similar to @sijo’s answer, you can just splat the pairs after a semicolon. This will work both for making NamedTuples or ComponentArrays directly:

```julia
julia> (; (Symbol.(n) .=> x)...)
(a = 1, b = 2, c = 3)

julia> ComponentArray(; (Symbol.(n) .=> x)...)
ComponentVector{Int64}(a = 1, b = 2, c = 3)

```
