# All the ways to do one-hot encoding

**URL:** <https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807>\
**Category:** General Usage\
**Created:** [July 17, 2021, 10:42am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807 "2021-07-17T10:42:01Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [July 17, 2021, 10:52am UTC](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807/2 "2021-07-17T10:52:18Z")

</div>

```julia
julia> using StatsBase

julia> x = ["a", "b", "c", "a", "b", "d"]
6-element Vector{String}:
 "a"
 "b"
 "c"
 "a"
 "b"
 "d"

julia> indicatormat(x)
4×6 Matrix{Bool}:
 1 0 0 1 0 0
 0 1 0 0 1 0
 0 0 1 0 0 0
 0 0 0 0 0 1

```

but normally I do your option 1 😃 :

```julia
julia> df = DataFrame(x=x)
6×1 DataFrame
 Row │ x
     │ String
─────┼────────
   1 │ a
   2 │ b
   3 │ c
   4 │ a
   5 │ b
   6 │ d

julia> select(df, [:x => ByRow(isequal(v))=> Symbol(v) for v in unique(df.x)])
6×4 DataFrame
 Row │ a b c d
     │ Bool Bool Bool Bool
─────┼────────────────────────────
   1 │ true false false false
   2 │ false true false false
   3 │ false false true false
   4 │ true false false false
   5 │ false true false false
   6 │ false false false true

```

(DataConvenience.jl is nice 👍)

---

_[View the full topic](https://discourse.julialang.org/t/all-the-ways-to-do-one-hot-encoding/64807)._
