‘&’ performs element-wise operation. You can use the & operator to evaluate AND across a vector.
‘&&’ examines only the first element of the operands. And resulting into a single length logical vector.
TRUE & c(TRUE,FALSE,FALSE)## [1] TRUE FALSE FALSE
TRUE && c(TRUE,FALSE,FALSE)## [1] TRUE
x<-c(TRUE,FALSE,.6,0)
y<-c(FALSE,TRUE,TRUE,FALSE)
x & y## [1] FALSE FALSE TRUE FALSE
x && y## [1] FALSE
‘|’ performs element-wise operation. You can use the | operator to evaluate OR across a vector.
‘||’ examines only the first element of the operands. And resulting into a single length logical vector.
TRUE | c(TRUE,FALSE,FALSE)## [1] TRUE TRUE TRUE
TRUE || c(TRUE,FALSE,FALSE)## [1] TRUE
x<-c(TRUE,FALSE,.6,0)
y<-c(FALSE,TRUE,TRUE,FALSE)
x | y## [1] TRUE TRUE TRUE FALSE
x || y## [1] TRUE
Arithmetic has an order of operations and so do logical expressions. All AND operators are evaluated before OR operators.
1 != 2 || 2 > 0 && 4 > 3.9## [1] TRUE
TRUE == FALSE && 1 != 0 || 4 < 3.9## [1] FALSE
99.99 > 100 || 45 < 7.3 || 4 != 4.0## [1] FALSE
TRUE && FALSE || 9 >= 4 && 3 < 6## [1] TRUE
The function isTRUE() takes one argument. If that argument evaluates to TRUE, the function will return TRUE. Otherwise, the function will return FALSE.
isTRUE(8 != 5)## [1] TRUE
!isTRUE(FALSE)## [1] TRUE
!isTRUE(!FALSE)## [1] FALSE
The function identical() will return TRUE if the two R objects passed to it as arguments are identical.
identical('R','Python')## [1] FALSE
identical(5>0,3>2.9)## [1] TRUE
identical(1,1.0)## [1] TRUE
The xor() function, which takes two arguments. The xor() function stands for exclusive OR. If one argument evaluates to TRUE and one argument evaluates to FALSE, then this function will return TRUE, otherwise it will return FALSE.
xor(!!TRUE, !!FALSE)## [1] TRUE
xor(4 >= 9, 8 != 8.0)## [1] FALSE
The which() function takes a logical vector as an argument and returns the indices of the vector that are TRUE.
x <- sample(10)
x## [1] 6 1 10 9 5 7 3 4 2 8
x > 5## [1] TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE TRUE
which(x > 5)## [1] 1 3 4 6 10
The functions any() and all() take logical vectors as their argument. The any() function will return TRUE if one or more of the elements in the logical vector is TRUE. The all() function will return TRUE if every element in the logical vector is TRUE.
all(x>0)## [1] TRUE
any(x<0)## [1] FALSE
f<-function(a,b){
a+b
}
f(2,3)## [1] 5
# If you want to see the source code for any function, just type the function name without any arguments or parentheses.
f## function(a,b){
## a+b
## }
You can also explicitly specify arguments in a function. When you explicitly designate argument values by name, the ordering of the arguments becomes unimportant. You can try this out by typing: remainder(divisor = 11, num = 5).
remainder<-function(a,b){
a%%b
}
remainder(b=5,a=11)## [1] 1
You can use the args() function to see a function’s arguments.
args(remainder)## function (a, b)
## NULL
You may be surprised to learn that you can pass a function as an argument without first defining the passed function. Functions that are not named are appropriately known as anonymous functions.
evaluate<-function(func,a){
func(a)
}
evaluate(function(x){x+1},3) # Here, the first argument is a tiny anonymous function that takes one argument `x` and returns `x+1`## [1] 4
You’re familiar with adding, subtracting, multiplying, and dividing numbers in R. To do this you use the +, -, *, and / symbols. These symbols are called binary operators because they take two inputs, an input from the left and an input from the right.
In R you can define your own binary operators. User-defined binary operators have the following syntax: %[whatever]% where [whatever] represents any valid variable name.
# Let's say I wanted to define a binary operator that multiplied two numbers and
# then added one to the product.
'%mult_add_one%'<-function(left,right){
left*right+1
}
4%mult_add_one%5## [1] 21