Case When in Julia

case-when functionality via the ternary operator

Author
Published

May 28, 2024

As far as I can tell, DataFrames.jl doesn’t have a built-in case-when function, but I found this post from Bogumil Kaminski showing how to implement case-when via the ternary operator.

Here’s the basic idea, but it’s also extensible to include multiple variables or to include missing values.

using DataFrames

X = DataFrame(a = 1:10)
10×1 DataFrame
Row a
Int64
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
transform!(
    X,
    :a => ByRow(a -> a % 2 == 0 ? "foo" : a % 3 == 0 ? "bar" : "baz") => :y
);

first(X, 5)
5×2 DataFrame
Row a y
Int64 String
1 1 baz
2 2 foo
3 3 bar
4 4 foo
5 5 baz