Jaskeil
Jaskeil

Reputation: 1232

Dplyr equivalent to CASE WHEN IS NOT NULL?

I am looking to create some logic that would look as such in SQL

CASE WHEN COLUMN IS NOT NULL THEN 1 ELSE 0 END 

How would this look like within the mutate verb using dplyr?

I was thinking

mutate(Bucket = case_when(!is.na(COLUMN) ~ 1,0))

Would this work

Upvotes: 0

Views: 1109

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521168

I would just use if_else here:

mutate(Bucket = if_else(!is.na(COLUMN), 1, 0))

The case_when function would be better suited to a SQL CASE expression having more than one logical branch.

Upvotes: 3

Related Questions