# CASE WHEN style operation on DataFrames

**URL:** https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414
**Category:** General Usage
**Tags:** dataframes
**Created:** [June 23, 2021, 2:42am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414 "2021-06-23T02:42:52Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ismam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ismam/32/26140_2.png) [@Ismam](https://discourse.julialang.org/u/Ismam)
#### Post date: [June 23, 2021, 2:42am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/1 "2021-06-23T02:42:52Z")

</div>

In SQL & R(dplyr) there’s a “case when” style function.  
I’m trying to do the equivalent in Julia.

For example suppose I have:

df = DataFrame(A = 1:4, B = [“M”, “F”, “F”, “M”])

4 rows × 2 columns

| | A | B |
| --- | --- | --- |
| | Int64 | String |
| 1 | 1 | M |
| 2 | 2 | F |
| 3 | 3 | F |
| 4 | 4 | M |

In R, I would be able to do:

> library(dplyr)
> 
> df = data.frame(A = 1:4,  
> B = c(“M”, “F”, “F”, “M”))
> 
> df2 = mutate(df, newcol = case\_when(  
> A == 2 & B == “F” ~ “bingo”,  
> TRUE ~ “no bingo”))

And it would produce a table as such:  
 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/b/1/b1ae73236a0c267e5be10d6277dd39a09fad28c8.png)

I’ve tried Match.jl but it doesn’t seem to broadcast easily to each row.  
Both Match.jl and the map function don’t seem to be able to do a test on two columns at the same time (see in the R code how I was able to use an and statement to join a condition on column A and B: A == 2 & B == “F”)

Vectorisation is always nice, buy is it simpler to just use a for loop? I heard Julia doesn’t suffer so much of a performace hit from for loops like languages like Python & R.

Many many thanks!

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [June 23, 2021, 3:05am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/2 "2021-06-23T03:05:27Z")

</div>

> **[Is case\_when needed in DataFrames.jl?](https://bkamins.github.io/julialang/2020/12/18/casewhen.html)**
>
> Introduction

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 23, 2021, 3:15am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/3 "2021-06-23T03:15:57Z")

</div>

> [@Ismam](#):
>
> df = DataFrame(A = 1:4, B = [“M”, “F”, “F”, “M”])

`“` this makes it hard to copy and paste code.

---

<div class="post-metadata">

### Author: ![Ismam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ismam/32/26140_2.png) [@Ismam](https://discourse.julialang.org/u/Ismam)
#### Post date: [June 23, 2021, 4:26am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/4 "2021-06-23T04:26:33Z")

</div>

The composer panel does it automatically.  
I put in straight quotes " and it autoformats it.  
How do I stop this behaviour? I tried to **escape** it

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 23, 2021, 4:33am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/5 "2021-06-23T04:33:18Z")

</div>

> [@Ismam](#):
>
> The composer panel does it automatically.

wow. i thought u put into word and then copied here. haha

also put ``` around code like this

```julia
df = DataFrame(A = 1:4, B = [“M”, “F”, “F”, “M”])

```

makes it stand out and easier to read.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [June 23, 2021, 5:54am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/6 "2021-06-23T05:54:25Z")

</div>

> [@Ismam](#):
>
> df2 = mutate(df, newcol = case\_when(  
> A == 2 & B == “F” ~ “bingo”,  
> TRUE ~ “no bingo”))

With DataFrameMacros.jl for example, you could use the ternary expression from Bogumils post relatively clearly:

```julia
using DataFrameMacros

df2 = @transform(df, :newcol = :A == 2 && :B == "F" ? "bingo" : "no bingo")

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 23, 2021, 8:21am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/7 "2021-06-23T08:21:25Z")

</div>

Being the base-DataFrames-purist that I am I would do:

```julia
julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrame        
 Row │ A B      
     │ Int64 String 
─────┼───────────────
   1 │ 1 M      
   2 │ 2 F      
   3 │ 3 F      
   4 │ 4 M      

julia> df.newcol = ifelse.((df.A .== 2) .& (df.B .== "F"), "bingo", "no bingo"); df
4×3 DataFrame
 Row │ A B newcol   
     │ Int64 String String   
─────┼─────────────────────────
   1 │ 1 M no bingo 
   2 │ 2 F bingo    
   3 │ 3 F no bingo 
   4 │ 4 M no bingo 

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 23, 2021, 9:28am UTC](https://discourse.julialang.org/t/case-when-style-operation-on-dataframes/63414/8 "2021-06-23T09:28:21Z")

</div>

> [@nilshg](#):
>
> `ifelse.((d`

if else doesn’t scale. I like the ternary operator solution in the blog post
