-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vectorised switch (aka SQL DECODE) #1710
Comments
I think it would make more sense to work like a vectorised |
Note that |
FWIW, I am frequently also using
...and they are called a bit more in the style of |
I do a lot of SQL querying in Oracle and often find myself reaching for the
DECODE
SQL function (link), which is a bit like a lightweightCASE
statement, with simple replacements and an optional default replacement.I thought it might also be useful with
tbl_df
usingmutate
in R so I wrote adecode
function that similarly takes pairs of arguments like so:decode(x, target_1, replacement_1, target_2, replacement_2, ... ,optional_default
)And set it up to work with named arguments (because I'm lazy).
mtcars %>% mutate(cyl_name = decode(cyl,4,'four',6,'six',8,'eight'))
mtcars %>% mutate(cyl_name = decode(cyl,4,'four',6,'six','guzzler'))
mtcars %>% mutate(cyl_name = decode(cyl,4,four,6,six,guzzler))
mtcars %>% mutate(cyl_name = decode(cyl,4,four,6,six))
Is it worth submitting a pull request to this
decode
function? I'm sure I can improve what's below.I think adding a
decode
function reduces a bit of the need to solve this issue (#631) relating to a general purpose SQL-likeCASE
function.The text was updated successfully, but these errors were encountered: