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".
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
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>`
Is there a technical difference between = and -
Yes there is. This is what the help page of '='
says:
The operators <- and = assign into the
environment in which they are
evaluated. The operator <- can be used
anywhere, whereas the operator = is
only allowed at the top level (e.g.,
in the complete expression typed at
the command prompt) or as one of the
subexpressions in a braced list of
expressions.
With "can be used" the help file means assigning an object here. In a function call you can't assign an object with =
because =
means assigning arguments there.
Basically, if you use <-
then you assign a variable that you will be able to use in your current environment. For example, consider:
matrix(1,nrow=2)
This just makes a 2 row matrix. Now consider:
matrix(1,nrow<-2)
This also gives you a two row matrix, but now we also have an object called nrow
which evaluates to 2! What happened is that in the second use we didn't assign the argument nrow
2, we assigned an object nrow
2 and send that to the second argument of matrix
, which happens to be nrow.
Edit:
As for the edited questions. Both are the same. The use of =
or <-
can cause a lot of discussion as to which one is best. Many style guides advocate <-
and I agree with that, but do keep spaces around <-
assignments or they can become quite hard to interpret. If you don't use spaces (you should, except on twitter), I prefer =
, and never use ->
!
But really it doesn't matter what you use as long as you are consistent in your choice. Using =
on one line and <-
on the next results in very ugly code.
Difference between | and || in R
The second form is useful for short-circuiting, possibly avoiding otherwise lengthy computations or errors in the second (or subsequent in lengthier statements) condition.
In particular,
condition || lengthyComputation()
will resolve quickly in the event of condition
being TRUE
. For example,
system.time(TRUE || {Sys.sleep(1);TRUE})
user system elapsed
0 0 0
system.time(FALSE || {Sys.sleep(1);TRUE})
user system elapsed
0 0 1
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
Related Topics
Converting Nested List to Dataframe
What's the Differences Between & and &&, | and || in R
Operator == Inconsistent in Logical Columns in Data.Table
Is There a Better Alternative Than String Manipulation to Programmatically Build Formulas
How to Put a Geom_Sf Produced Map on Top of a Ggmap Produced Raster
Moving Average of Previous Three Values in R
How to Increase Font Size in a Plot in R
Why Is Enquo + !! Preferable to Substitute + Eval
How to Hide or Disable In-Function Printed Message
Why Does Unlist() Kill Dates in R
How to Deal with "'Somefunction' Is Not an Exported Object from 'Namespace:Somepackage'" Error
Create Dynamic Number of Input Elements with R/Shiny
Difference Between Passing Options in Aes() and Outside of It in Ggplot2
How Do Keep Only Unique Words Within Each String in a Vector