rsyntaxvectorizationlogical-operators

logicals operators long and short forms on vectors


I read many posts on SO about the difference between short and long form for logicals operators, but I need an explicit precision.

I read that :

The longer form evaluates left to right examining only the first element of each vector.

Then, is this :

c(TRUE, TRUE) && c(TRUE, NA)

strictly the same operation than this :

(c(TRUE, TRUE) & c(TRUE, NA))[1]

The result of the logical & between the 1st element of the 1st vector and the 1st element of the second vecor ?

If this is right, what is the interest of returning simply the value returned by the logical operator between the first element of each vector ?
I supposed it shouldn't be used with vectors then ?


Solution

  • ?'&&' gives the place that you want the longer form:

    The longer form is appropriate for programming control-flow and typically
    preferred in if clauses.
    

    if takes a single value. Unfortunately, && doesn't give a warning if a vector exceeds length 1 (if does give a warning) so it's harder to catch such a mistake.

    Indeed, you shouldn't be using && with vectors of length other than 1.