From 6410b4f9693427c730a701c27470d644825a6e71 Mon Sep 17 00:00:00 2001 From: Ian Lyttle Date: Thu, 10 Mar 2016 08:07:44 -0600 Subject: [PATCH] Functions.Rmd: some words on identical() Adds discussion of `identical()` function to `if` section. I, personally, find identical() to be useful, but I can see that it does not add aesthetic value to the code. (BTW, really enjoying seeing this book come together!) --- functions.Rmd | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/functions.Rmd b/functions.Rmd index 872080c..3dfc56b 100644 --- a/functions.Rmd +++ b/functions.Rmd @@ -279,6 +279,14 @@ if (NA) {} You can use `||` (or) and `&&` (and) to combine multiple logical expressions. These operators are "short-circuiting": as soon as `||` sees the first `TRUE` it returns `TRUE` without computing anything else. As soon as `&&` sees the first `FALSE` it returns `FALSE`. You should never use `|` or `&` in an `if` statement: these are vectorised operations that apply to multiple values. If you do have a logical vector, you can use `any()` or `all()` to collapse it to a single value. +You can enforce this more-strictly by using the `identical()` function, which returns either a single `TRUE` or a single `FALSE`. One thing to watch out for is that *you* have to be more strict when specifying integers: + +```{r} +if (i == 0){} + +if (identical(i, 0L)){} +``` + ### Multiple conditions You can chain multiple if statements together: