# How to index a \`CatagoricalArray\` then make a new array with the same levels

**URL:** <https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874>\
**Category:** Data\
**Created:** [August 4, 2018, 1:06am UTC](https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874 "2018-08-04T01:06:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 4, 2018, 1:06am UTC](https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874/1 "2018-08-04T01:06:53Z")

</div>

I have been looking at the source code for `CategoricalArray` but I can’t figure this out.

```julia
vec = CategoricalArray([1,2,1,2])
t = vec[1]
# We know what levels 
t.pool # CategoricalArrays.CategoricalPool{Int64,UInt32}([1,2])
newvec = CategoricalArray(fill(t, 5))
newvec.pool # CategoricalArrays.CategoricalPool{Int64,UInt32}([1])
# not [1, 2]

```

Because `newvec.pool` is immutable, you can’t alter it after you create the vector.

Anyone have good enough knowledge of the constructors for `CategoricalArrays` to help me with this?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [August 8, 2018, 4:42pm UTC](https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874/2 "2018-08-08T16:42:14Z")

</div>

You can just do `levels!(newvec, levels(vec))`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 8, 2018, 5:25pm UTC](https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874/3 "2018-08-08T17:25:29Z")

</div>

Thanks! I’ll add this to my code.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 12, 2018, 12:41am UTC](https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874/4 "2018-08-12T00:41:32Z")

</div>

Just got around to doing this, sorry.

I’m sorry i wasn’t clear earlier. I needed an array where all you are given is a `CategoricalValue`, not the full array. The solution i ended up using was

```julia
levels!(vec, CategoricalArrays.index(CategoricalArrays.pool(x)))

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [August 15, 2018, 5:51pm UTC](https://discourse.julialang.org/t/how-to-index-a-catagoricalarray-then-make-a-new-array-with-the-same-levels/12874/5 "2018-08-15T17:51:59Z")

</div>

OK. Rather do `levels!(vec, levels(CategoricalArrays.pool(x)))`. Using `index` instead won’t use the correct ordering of levels in some cases (the ordering of the index is an implementation detail).
