“When you have trouble with things—whether it’s figuring out whether to push or pull a door or the arbitrary vagaries of the modern computer and electronics industries—it’s not your fault. Don't blame yourself: blame the designer...” — Donald A. Norman
today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade") c(today, lunch) What happens when you combine a date and a date-time?
today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade") c(today, lunch) #> [1] "2018-09-18" #> [2] "4210927-01-24" What happens when you combine a date and a date time? WAT!
today <- as.Date("2018-09-18") lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade") c(lunch, today) What happens when you combine a date and a date-time?
lunch <- as.POSIXct("2018-09-18 12:00", tz = "Europe/Belgrade") lunch #> [1] "2018-09-18 12:00:00 CEST" c(lunch) c(NULL, lunch) What happens if you look at a date-time the wrong way?
c(, ) -> c(>) -> c(NULL, >) -> Types can give us a high-level overview of a function I’ll put types in angle brackets to make clear that this is not real R code
For atomic vectors, the rules are simple Logical Integer Double Character Logical logical integer double character Integer integer integer double character Double double double double character Character character character character character
For atomic vectors, the rules are simple Logical Integer Double Character Even if you’re never explicitly learned this, I think you internalise it quickly.
if_else(, , ) -> if_else(, , ) -> if_else(, , ) -> ??? if_else(, , ) -> ??? if_else(, , ) -> ??? if_else(, , ) -> ??? But there are a few that are more complex
x <- runif(6) if_else(x > 0.5, x, NA) #> Error: `false` must be type double, #> not logical if_else(x > 5, x, NA_real_) #> [1] NA 0.700 0.557 NA NA NA Which leads to this annoyance You’re currently forced to learn about the “typed” NAs
Practitioner Programmer Implicit Explicit Interactive Easily detect & resolve problems Packaged In production Code is a conversation Ambiguity can be tolerated early and often