# Ifelse and datawrangling

**URL:** <https://discourse.julialang.org/t/ifelse-and-datawrangling/82530>\
**Category:** New to Julia\
**Tags:** control, dataframes\
**Created:** [June 9, 2022, 9:48pm UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530 "2022-06-09T21:48:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wub7quxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wub7quxa/32/33473_2.png) [@wub7quxa](https://discourse.julialang.org/u/wub7quxa)\
**Post date:** [June 9, 2022, 9:48pm UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/1 "2022-06-09T21:48:56Z")

</div>

Let’s say I have the dataframe

```julia
using DataFrames
a1=DataFrame(A=1:3, B=5:7)

```

and I want to replace 1 by USA, 2 by MEX, and 3 by CAN in the first column. I can do

```julia
a2=hcat(
    ifelse.(a1[!,[:A]].==1,"USA",
    ifelse.(a1[!,[:A]].==2,"MEX",
    ifelse.(a1[!,[:A]].==3,"CAN",
    "ERROR"))),select(a1,:B)
    )

```

This has exactly the effect that I want. However, I would like to issue this command using two vectors x=[1;2;3] and y=[“USA”,“MEX”,“CAN”] in order to make it more flexible, so I don’t have to change the code when these vectors change. Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [June 9, 2022, 10:16pm UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/2 "2022-06-09T22:16:43Z")

</div>

You might want to look at CategoricalArrays.jl, and the `recode!()` function. I think that interface is really nice and intuitive, and designed for exactly this kind of situation.

Another approach that doesn’t use another package would be to use a dictionary.

```julia
d = Dict(k => v for (k, v) in zip(1:3, ("USA", "MEX", "CAN")))

a1.label = [d[n] for n in a1.A]

```

---

<div class="post-metadata">

**Author:** ![wub7quxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wub7quxa/32/33473_2.png) [@wub7quxa](https://discourse.julialang.org/u/wub7quxa)\
**Post date:** [June 10, 2022, 1:43am UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/3 "2022-06-10T01:43:36Z")

</div>

Thank you @kevbonham – just what I was looking for!

---

<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:** [June 10, 2022, 6:13am UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/4 "2022-06-10T06:13:25Z")

</div>

> [@kevbonham](#):
>
> `d = Dict(k => v for (k, v) in zip(1:3, ("USA", "MEX", "CAN")))`

or

```julia
d = Dict(enumerate(["USA", "MEX", "CAN"]))

```

---

<div class="post-metadata">

**Author:** ![abobroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abobroff/32/3752_2.png) [@abobroff](https://discourse.julialang.org/u/abobroff)\
**Post date:** [June 10, 2022, 6:47am UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/5 "2022-06-10T06:47:56Z")

</div>

` insertcols!(a1,:label => ["USA", "MEX", "CAN"][a1.A])`

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 10, 2022, 8:47pm UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/6 "2022-06-10T20:47:59Z")

</div>

Another variant:

```julia
using DataFrames

a1 = DataFrame(A=1:4, B=5:8)

x = [1; 2; 3]
y = ["USA", "MEX", "CAN"]
d = Dict(x .=> y)
a1[!,1] = get.(Ref(d), a1[:,1], missing)
a1

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [June 10, 2022, 10:05pm UTC](https://discourse.julialang.org/t/ifelse-and-datawrangling/82530/7 "2022-06-10T22:05:06Z")

</div>

> [@rafael.guerra](#):
>
> Another variant

Ooh, handling the missing key, I dig it!
