# If, elseif, else vs Case When

**URL:** <https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 10, 2021, 12:04pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978 "2021-09-10T12:04:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)\
**Post date:** [September 10, 2021, 12:04pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978/1 "2021-09-10T12:04:27Z")

</div>

I was trying to get a multiple conditional statement running and I keep hitting an error.

```julia
df1 = @chain df begin
    @transform(:Business_Type = if :Business_Type .== "Corporate Travel Department (CTD)"
                                   :Business_Type ="Corporate"
                                elseif :Business_Type .== "Cruise" 
                                   :Business_Type = "Leisure"
                                elseif :Business_Type .== "Corporate"  
                                    :Business_Type ="Corporate"
                                elseif :Business_Type .== "Leisure" 
                                    :Business_Type = "Leisure"
                                elseif :Business_Type .== "Travel Management Company" 
                                    :Business_Type ="Unkown"
                                elseif :Business_Type .== "Tour Operator"
                                    :Business_Type ="Unknown"
                                else "Unknown"
                                end)
    @select(:Business_Type, :trip_type, :travellers, :haul, :cabin_class, :legs, :travel_dow, :businessday
    ,:trip_length_days, :lead_time, :businessdaytotal, :travel_month, :DBA_Name, :iata, :pcc)
end

```

The error message is:

```julia
ERROR: TypeError: non-boolean (Vector{Union{Missing, Bool}}) used in boolean context

```

The example I used was:

```julia
if g == 0
        0
    elseif g == 1 
        10
    elseif g == 2
        20
    elseif g == 3
        30
    elseif g == 4
        40
    else
        50
    end

```

I think the complication I’m having is dealing with casting it to every row and doing it within a @transform statement… I was looking at the @byrow command but it seems like it can only use one argument:

```julia
@transform(df, @byrow :y = :x == 1 ? true : false)

```

> `@byrow` accepts a single argument and creates an anonymous function wrapped in `ByRow`

Thoughts?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 10, 2021, 12:07pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978/2 "2021-09-10T12:07:06Z")

</div>

> [@Billpete002](#):
>
> `.==`

Why are you trying to use a dot here? That will return a vector, and this is the cause of the error.

---

<div class="post-metadata">

**Author:** ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)\
**Post date:** [September 10, 2021, 12:56pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978/3 "2021-09-10T12:56:13Z")

</div>

Ah I tried it earlier and it only produced the else part of the code, i.e. all of the results ended up as “unknown” when I know that there should be “corporate” or “leisure”

```julia
julia> levels(df1.Business_Type)
1-element Vector{String}:
 "Unknown"

```

---

<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:** [September 10, 2021, 1:39pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978/4 "2021-09-10T13:39:13Z")

</div>

There are a few tweaks to your code in DataFramesMeta that will make this easier

1. `@rtransform` instead of `@transform`. This operates row-wise (`else-if` stuff doesn’t work on vectors.
2. No need to use parentheses. IMO it’s easier with a `begin...end` block. The non-parentheses part is most important when it comes to skipping over missing values.

```julia

julia> @chain df begin 
           @rtransform :Business_Type = begin 
               if :Business_Type == "Corporate" 
                   1
               elseif :Business_Type == "Leisure"
                   2
               elseif :Business_Type == "Cruise"
                   3
               end
           end
       end

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [September 10, 2021, 1:59pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978/5 "2021-09-10T13:59:04Z")

</div>

You may also want to consider a `Dict` instead of a large if-else block, e.g.

```julia
julia> departments = Dict("Corporate Travel Department (CTD)" => "Corporate",
                          "Cruise" => "Leisure",
                          "Corporate" => "Corporate",
                          "Travel Management Company" => "Unknown",
                          "Tour Operator" => "Unknown")

julia> df = DataFrame(Business_Type = rand(["Corporate Travel Department", "Cruise", "Corporate", "Leisure", "Tour Operator"], 5), 
                      id=rand(UInt8, 5))
5×2 DataFrame
 Row │ Business_Type id
     │ String UInt8
─────┼────────────────────────────────────
   1 │ Cruise 10
   2 │ Corporate 178
   3 │ Corporate 199
   4 │ Corporate 131
   5 │ Corporate Travel Department 152

julia> @rtransform(df, :Business_Type = get(departments, :Business_Type, "Unknown"))
5×2 DataFrame
 Row │ Business_Type id
     │ String UInt8
─────┼──────────────────────
   1 │ Leisure 10
   2 │ Corporate 178
   3 │ Corporate 199
   4 │ Corporate 131
   5 │ Unknown 152

```

---

<div class="post-metadata">

**Author:** ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)\
**Post date:** [September 10, 2021, 2:37pm UTC](https://discourse.julialang.org/t/if-elseif-else-vs-case-when/67978/6 "2021-09-10T14:37:45Z")

</div>

Unfortunately I can’t mark multiple solutions but thank you!
