CASE WHEN style operation on DataFrames

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

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!

2 Likes

this makes it hard to copy and paste code.

1 Like

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

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

also put ``` around code like this

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

makes it stand out and easier to read.

1 Like

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

using DataFrameMacros

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

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

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 

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

2 Likes