3.4 Case Statement
20200421 Many languages support a case or switch statement. R did not originally have a case statement but it was introduced through tidyr as dplyr::case_when():
library(dplyr)
with(ds %>% sample_n(100),
case_when(max_temp > 35 ~ "hot",
max_temp > 25 ~ "warm",
max_temp > 18 ~ "mild",
max_temp > 12 ~ "cool",
max_temp > 0 ~ "cold",
TRUE ~ "freezing"))
## [1] "warm" "warm" "warm" "warm" "warm" "mild"
## [7] "warm" "warm" "cold" "mild" "mild" "warm"
## [13] "mild" "warm" "mild" "cool" "mild" "cold"
## [19] "cool" "warm" "mild" "mild" "mild" "warm"
## [25] "mild" "warm" "mild" "mild" "mild" "mild"
## [31] "warm" "cool" "freezing" "mild" "mild" "warm"
## [37] "mild" "cool" "mild" "mild" "mild" "mild"
## [43] "warm" "warm" "warm" "mild" "mild" "mild"
## [49] "mild" "mild" "mild" "hot" "mild" "warm"
## [55] "hot" "warm" "mild" "hot" "cool" "cold"
## [61] "warm" "cold" "hot" "warm" "cool" "cool"
## [67] "warm" "mild" "mild" "mild" "cold" "cool"
## [73] "mild" "cool" "warm" "cold" "warm" "warm"
## [79] "cool" "mild" "mild" "warm" "mild" "warm"
## [85] "warm" "cool" "mild" "mild" "warm" "hot"
## [91] "mild" "warm" "warm" "mild" "warm" "cool"
## [97] "mild" "mild" "cool" "warm"
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0