According to the R language definition, the difference between &
and &&
(correspondingly |
and ||
) is that the former is vectorized while the latter is not.
According to the help text, I read the difference akin to the difference between an "And" and "AndAlso" (correspondingly "Or" and "OrElse")... Meaning: That not all evaluations if they don't have to be (i.e. A or B or C is always true if A is true, so stop evaluating if A is true)
Could someone shed light here? Also, is there an AndAlso and OrElse in R?
The answer about "short-circuiting" is potentially misleading, but has some truth (see below). In the R/S language,
&&
and||
only evaluate the first element in the first argument. All other elements in a vector or list are ignored regardless of the first ones value. Those operators are designed to work with theif (cond) {} else{}
construction and to direct program control rather than construct new vectors.. The&
and the|
operators are designed to work on vectors, so they will be applied "in parallel", so to speak, along the length of the longest argument. If the vectors are not the same length then recycling of the shorter argument is performed.When the arguments to
&&
or||
are evaluated, there is "short-circuiting" in that if any of the values in succession from left to right are determinative, then evaluations cease and the final value is returned.&&
and||
are what is called "short circuiting". That means that they will not evaluate the second operand if the first operand is enough to determine the value of the expression.For example if the first operand to
&&
is false then there is no point in evaluating the second operand, since it can't change the value of the expression (false && true
andfalse && false
are both false). The same goes for||
when the first operand is true.You can read more about this here: http://en.wikipedia.org/wiki/Short-circuit_evaluation From the table on that page you can see that
&&
is equivalent toAndAlso
in VB.NET, which I assume you are referring to.The shorter ones are vectorized, meaning they can return a vector, like this:
The longer form evaluates left to right examining only the first element of each vector, so the above gives
As the help page says, this makes the longer form "appropriate for programming control-flow and [is] typically preferred in if clauses."
So you want to use the long forms only when you are certain the vectors are length one.
You should be absolutely certain your vectors are only length 1, such as in cases where they are functions that return only length 1 booleans. You want to use the short forms if the vectors are length possibly >1. So if you're not absolutely sure, you should either check first, or use the short form and then use
all
andany
to reduce it to length one for use in control flow statements, likeif
.The functions
all
andany
are often used on the result of a vectorized comparison to see if all or any of the comparisons are true, respectively. The results from these functions are sure to be length 1 so they are appropriate for use in if clauses, while the results from the vectorized comparison are not. (Though those results would be appropriate for use inifelse
.One final difference: the
&&
and||
only evaluate as many terms as they need to (which seems to be what is meant by short-circuiting). For example, here's a comparison using an undefined valuea
; if it didn't short-circuit, as&
and|
don't, it would give an error.Finally, see section 8.2.17 in The R Inferno, titled "and and andand".