The difference between & and && in R
&
is a logical operator so R coverts your quantities to logical values before comparison. For numeric values any non-0 (and non-NA/Null/NaN stuff) gets the value TRUE and 0 gets FALSE. So with that said things make quite a bit of sense
> as.logical(c(1,2,3))
[1] TRUE TRUE TRUE
> as.logical(c(1,3,3))
[1] TRUE TRUE TRUE
> as.logical(c(1,2,3)) & as.logical(c(1,2,3))
[1] TRUE TRUE TRUE
> as.logical(c(1,2,3)) & as.logical(c(1,3,3))
[1] TRUE TRUE TRUE
Boolean operators && and ||
The shorter ones are vectorized, meaning they can return a vector, like this:
((-2:2) >= 0) & ((-2:2) <= 0)
# [1] FALSE FALSE TRUE FALSE FALSE
The longer form evaluates left to right examining only the first element of each vector, so the above gives
((-2:2) >= 0) && ((-2:2) <= 0)
# [1] FALSE
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
and any
to reduce it to length one for use in control flow statements, like if
.
The functions all
and any
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 in ifelse
.
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 value a
; if it didn't short-circuit, as &
and |
don't, it would give an error.
a
# Error: object 'a' not found
TRUE || a
# [1] TRUE
FALSE && a
# [1] FALSE
TRUE | a
# Error: object 'a' not found
FALSE & a
# Error: object 'a' not found
Finally, see section 8.2.17 in The R Inferno, titled "and and andand".
In R programming, what's the difference between & vs &&, and | vs ||
they can only handle a single logical test on each side of the operator
a <- c(T, F, F, F)
b <- c(T, F, F, F)
a && b
Returns
[1] TRUE
Because only the first element of a
and b
are tested!
Edit:
Consider the following, where we 'rotate' a
and b
after each &&
test:
a <- c(T, F, T, F)
b <- c(T, F, F, T)
for (i in seq_along(a)){
cat(paste0("'a' is: ", paste0(a, collapse=", "), " and\n'b' is: ", paste0(b, collapse=", "),"\n"))
print(paste0("'a && b' is: ", a && b))
a <- c(a[2:length(a)], a[1])
b <- c(b[2:length(b)], b[i])
}
Gives us:
'a' is: TRUE, FALSE, TRUE, FALSE and
'b' is: TRUE, FALSE, FALSE, TRUE
[1] "'a && b' is: TRUE"
'a' is: FALSE, TRUE, FALSE, TRUE and
'b' is: FALSE, FALSE, TRUE, TRUE
[1] "'a && b' is: FALSE"
'a' is: TRUE, FALSE, TRUE, FALSE and
'b' is: FALSE, TRUE, TRUE, FALSE
[1] "'a && b' is: FALSE"
'a' is: FALSE, TRUE, FALSE, TRUE and
'b' is: TRUE, TRUE, FALSE, TRUE
[1] "'a && b' is: FALSE"
Additionally, &&
, ||
stops as soon as the expression is clear:
FALSE & a_not_existing_object
TRUE | a_not_existing_object
Returns:
Error: object 'a_not_existing_object' not found
Error: object 'a_not_existing_object' not found
But:
FALSE && a_not_existing_object
TRUE || a_not_existing_object
Returns:
[1] FALSE
[1] TRUE
Because anything after FALSE
AND something (and TRUE
OR something) becomes FALSE
and TRUE
respectively
This last behavior of &&
and ||
is especially useful if you want to check in your control-flow for an element that may not exist:
if (exists(a_not_existing_object) && a_not_existing_object > 42) {...}
This way the evaluation stops after the first expression evaluates to FALSE
and the a_not_existing_object > 42
part is not even atempted!
& and && behaving very differently in R
The '&' here returns a vector, It is 'and' operator between each pair of 1 <= mhbins$val
and mhbins$val <= 7
While '&&' looks at only first pair of 1 <= mhbins$val
and mhbins$val <= 7
Example
c(TRUE,TRUE) & c(FALSE,TRUE) `returns <[1] FALSE TRUE>`
c(TRUE,TRUE) && c(FALSE,TRUE) `returns <[1] FALSE>`
What is the difference between short (&,|) and long (&&, ||) forms of AND, OR logical operators in R?
&
and |
- are element-wise and can be used with vector operations, whereas, ||
and &&
always generate single TRUE
or FALSE
theck the difference:
> x <- 1:5
> y <- 5:1
> (x > 2) & (y < 3)
[1] FALSE FALSE FALSE TRUE TRUE
> (x > 2) && (y < 3) # here operaand && takes only 1'st elements from logical
# vectors (x>2) and (y<3)
> FALSE
So, &&
and ||
are commonly used in if (condition) state_1 else state_2
statements, as
dealing with vectors of length 1
What is the difference and what are the & and && operators used for?
The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator.
Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands. The & represents a bitwise AND operation- where A and B represent two inputs; both inputs must be true in order for C to be true.
So for instance in the example you provided you have:
if(0 & 1){
}
The result of the bitwise AND on 0 and 1 as inputs is 0 (false) because both inputs must be true for the output to be true.
The && operator is a logical operator, which is used to make a decision based on multiple conditions. It can apply to one or two operands each of which may be true or false.
if (!a && b < 5) {
}
The above still evaluates to false because both conditions must be met for the code inside the if statement to be executed. In addition to this, using the && operator will (depending on the language) short circuit, which means that the second condition of the if will only be evaluated if the outcome is not determined by the first condition.
So this expression will fail as soon as a is found to be zero (false) because there is no point in evaluating the second expression as the first expression had to be true. This is another difference as the bitwise & operator does not short circuit.
Related Topics
Interleave Columns of Two Data Frames
Bar Plot for Count Data by Group in R
Reshape R Data with User Entries in Rows, Collapsing for Each User
Click on Cross Domain Iframe Element Using Rselenium
Populate Nas in a Vector Using Prior Non-Na Values
Adding Manual Legend in Ggplot
Get First Entries in Rows of List
Ggplot2: How to Rotate a Graph in a Specific Angle
Combining .Sd with Renamed Variable Messes with Names of .Sd Columns
Create a New Column with Non-Null Columns' Names
Understanding Bandwidth Smoothing in Ggplot2
Counting the Number of Values Greater Than 0 in R in Multiple Columns
Function for Polynomials of Arbitrary Order (Symbolic Method Preferred)
Shiny Leaflet Easyprint Plugin
Error When Mapping in Ggmap with API Key (403 Forbidden)
Changing the Order of Dodged Bars in Ggplot2 Barplot
Add New Value to New Column Based on If Value Exists in Other Dataframe in R