Skip to content

Instantly share code, notes, and snippets.

@mmparker
Last active November 2, 2016 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmparker/2ed0d441805047da2d5cf5f20f62ddfe to your computer and use it in GitHub Desktop.
Save mmparker/2ed0d441805047da2d5cf5f20f62ddfe to your computer and use it in GitHub Desktop.
When working with a sqlite tbl, if_else() won't accept named arguments - but *will* accept unnamed.
library(dplyr)
# Set up a temp sqlite database
db <- src_sqlite(tempfile(), create = TRUE)
iris2 <- copy_to(db, iris)
# if_else with named true and false on a data.frame -> no problem
iris %>% mutate(Sepal.Size = if_else(Sepal.Length > 5,
true = "big",
false = "not big"))
# if_else with named true and false on the sqlite tbl ->
# Error in if_else(Sepal.Length > 5, true = "big", false = "not big") :
# unused arguments (true = "big", false = "not big")
iris2 %>% mutate(Sepal.Size = if_else(Sepal.Length > 5,
true = "big",
false = "not big"))
# it works if unnamed, however
iris2 %>% mutate(Sepal.Size = if_else(Sepal.Length > 5,
"big",
"not big"))
# Same problem with base::ifelse
iris2 %>% mutate(Sepal.Size = ifelse(Sepal.Length > 5,
yes = "big",
no = "not big"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment