[R] if documentation
PIKAL Petr
petr@p|k@| @end|ng |rom prechez@@cz
Wed Dec 7 13:40:41 CET 2022
Hallo Martin
I understand. Anyway, it would not do any harm to add something like
x <- 11
if(x < 0) {print("weird")} else if(x >= 0 & x <= 9) {
print("too low")} else if (x > 9 & x <= 10) {
print("just enough")} else {
print("more than expected")}
to the help page for if.
Regards
Petr
> -----Original Message-----
> From: Martin Maechler <maechler using stat.math.ethz.ch>
> Sent: Wednesday, December 7, 2022 12:16 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>
> Cc: R-help Mailing List <r-help using r-project.org>
> Subject: Re: [R] if documentation
>
> >>>>> PIKAL Petr
> >>>>> on Wed, 7 Dec 2022 07:04:38 +0000 writes:
>
> > Hallo all Not sure if it is appropriate place but as I am
> > not involved in r-devel list I post here.
>
>
>
> > Documentation for Control (if, for, while, .) is missing
> > "if else" command. Although it can be find online
> > elsewhere I believe that adding it either as an example or
> > as a third entry and paragraph about nested if's could be
> > beneficial.
>
>
>
> > if(cond) cons.expr else if (cond) alt.expr else alt2.expr
>
> > Nested if expressions are better realized with "else if"
> > instead of sequence of plain "else" control statements
> > especially when using several of them.
>
> I agree. However there is no "else if" special.
>
> As everything that *happens* in R is a function call (John Chambers),
indeed, `if`
> is a function too, with an unusual syntax which may involve `else`.
>
> If you look more closely, `if` is a function with 3 arguments, where the
last (3rd)
> is optional and has a default of NULL :
>
> Some code towards proving the above :
>
>
> (t1 <- if(TRUE) 1:3 ) # is identical to if(TRUE) 1:3 else NULL
> f1 <- if(FALSE) 1:3 # is identical to
> f2 <- if(FALSE) 1:3 else NULL
> identical(f1,f2)
> f3 <- if(FALSE) 1:3 else 111
>
> `if`(TRUE, 1:3)
> `if`(TRUE, 1:3, NULL)
>
> `if`(FALSE, 1:3) # returns invisibly
> `if`(FALSE, 1:3, NULL)
> `if`(FALSE, 1:3, 111)
>
> --------------
>
> So, 'if(.) else' or 'if(...) else if(..) ..'
> etc are
> all just versions of calling the `if` function sometimes, in a nested way.
More information about the R-help
mailing list