Skip to content

Instantly share code, notes, and snippets.

@kelly-sovacool
Last active September 22, 2020 19:05
Show Gist options
  • Save kelly-sovacool/38958966cb9c9389df40c6931b28d8d5 to your computer and use it in GitHub Desktop.
Save kelly-sovacool/38958966cb9c9389df40c6931b28d8d5 to your computer and use it in GitHub Desktop.
R's ifelse() doesn't behave as I first expected
pick_something <- function(choice) {
ifelse(choice == 'list',
list("here's", "your", "list"),
"this isn't a list")
}
pick_something('anything_else')
#> [1] "this isn't a list"
pick_something('list')
#> [[1]]
#> [1] "here's"
?base::ifelse
# The important part:
# ifelse returns a value with the same shape as test which is filled with elements selected from either
# yes or no depending on whether the element of test is TRUE or FALSE.
# This function is not interchangeable with Python's inline if-else statement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment