Last active
September 22, 2020 19:05
-
-
Save kelly-sovacool/38958966cb9c9389df40c6931b28d8d5 to your computer and use it in GitHub Desktop.
R's ifelse() doesn't behave as I first expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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