# How to change the type of a variable for a loaded table?

**URL:** <https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973>\
**Category:** Statistics\
**Tags:** question, juliadb\
**Created:** [January 4, 2020, 6:33pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973 "2020-01-04T18:33:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [January 4, 2020, 6:33pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/1 "2020-01-04T18:33:02Z")

</div>

I’m trying to use `JuliaDB` to load tables and then use `MixedModels` to fit models.

The `MixedModels` package requires the clustering variable “id” to be categorical, but in my dataset “id” is integer.

So my question is, how can I change the type of “id” to categorical after loading the table? One solution is to convert the table to a DataFrame and then change the type, but I’m sure there are better solutions.

---

<div class="post-metadata">

**Author:** ![Gunter\_Faes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunter_faes/32/12580_2.png) [@Gunter\_Faes](https://discourse.julialang.org/u/Gunter_Faes)\
**Post date:** [January 6, 2020, 9:10am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/2 "2020-01-06T09:10:51Z")

</div>

Maybe

```julia
categorical!(df::DataFrame, cols::Type=Union{AbstractString, Missing}; compress::Bool=false)

```

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [January 6, 2020, 10:56am UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/3 "2020-01-06T10:56:57Z")

</div>

You can do that with ScientificTypes fairly easily (actually that’s exactly the kind of use case ST was developed for)

```julia
julia> using ScientificTypes
julia> t = JuliaDB.table(1:10, randn(10), rand(Bool, 10); names = [:x, :y, :z]);
julia> t2 = coerce(t, :z => Multiclass)

```

In the background what that does is form a derived table where the column `:z` has been obtained by doing `categorical(select(t, z))`; if you wanted to have that as ordered categoricals, `coerce(t, :z => OrderedFactor)` does the trick. `t2` has the same type as `t`.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [January 6, 2020, 4:26pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/4 "2020-01-06T16:26:50Z")

</div>

What version of `MixedModels` are you using? I thought we changed the behavior so that a clustering variable did not need to be categorical.

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [January 6, 2020, 6:36pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/5 "2020-01-06T18:36:46Z")

</div>

Hi Thibaut, thank you for the reply. I’m running Julia v1.1 and just installed ScientificTypes v0.2.3. I ran the example you gave and got the error message “type IndexedTable has no field x”.

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [January 6, 2020, 6:42pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/6 "2020-01-06T18:42:14Z")

</div>

I’m running Julia v1.1 and MixedModels v2.1.2.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [January 6, 2020, 6:43pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/7 "2020-01-06T18:43:47Z")

</div>

Can you upgrade to v.2.2.0 and try again?

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [January 6, 2020, 6:46pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/8 "2020-01-06T18:46:47Z")

</div>

Sure. Looks like I need to upgrade Julia to v1.3 too. I’ll get back to you in a bit.

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [January 6, 2020, 7:38pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/9 "2020-01-06T19:38:28Z")

</div>

I’ve upgraded to MixedModels v2.2 and Julia v1.3.1, but it’s still asking the blocking variable to be categorical.

` **ERROR:** ArgumentError: blocking variables (those behind |) must be Categorical (id is not)`

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [January 6, 2020, 8:03pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/10 "2020-01-06T20:03:34Z")

</div>

You may have entered a typo, below is the exact script I just ran in a fresh environment

```julia
using JuliaDB, ScientificTypes

t = JuliaDB.table(1:10, randn(10), rand(Bool, 10); names = [:x, :y, :z]);

t2 = coerce(t, :z => Multiclass)

@show typeof(t2)
@show typeof(select(t2, :z))

```

Here’s the output

```julia
typeof(t2) = IndexedTable{StructArrays.StructArray{NamedTuple{(:x, :y, :z),Tuple{Int64,Float64,CategoricalArrays.CategoricalValue{Bool,UInt8}}},1,NamedTuple{(:x, :y, :z),Tuple{Array{Int64,1},Array{Float64,1},CategoricalArrays.CategoricalArray{Bool,1,UInt8,Bool,CategoricalArrays.CategoricalValue{Bool,UInt8},Union{}}}},Int64}}
typeof(select(t2, :z)) = CategoricalArrays.CategoricalArray{Bool,1,UInt8,Bool,CategoricalArrays.CategoricalValue{Bool,UInt8},Union{}}

```

I’m on Julia 1.3.1 with JuliaDB 0.13 and ScientificType 0.3.2

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [January 6, 2020, 8:16pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/11 "2020-01-06T20:16:45Z")

</div>

Thanks for checking.

---

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [January 6, 2020, 9:34pm UTC](https://discourse.julialang.org/t/how-to-change-the-type-of-a-variable-for-a-loaded-table/32973/12 "2020-01-06T21:34:36Z")

</div>

Thanks for checking. I upgraded to Julia v1.3.1 and copied the same code above and it works now. So it might have been a problem with v1.1. In any case, thanks for your help.
