Like other computer languages, we can use R to do logical judgments. In order to understand what and how they are, a number of logical operators are introduced here. For example, the logical negation (NOT) of a variable A can be indicated by using !A. The NOT operator will make a binary return, either TRUE or FALSE. In the convention of computer languages, the numbers 1 and 0 also represent the logical values TRUE and FALSE respectively. Thus, !1 should be FALSE and !0 should be TRUE. In R, TRUE and FALSE can be abbreviated as T and F.
A<-9
B<-c(1,4,5,8,10)
!A
## [1] FALSE
!B
## [1] FALSE FALSE FALSE FALSE FALSE
!1
## [1] FALSE
!0
## [1] TRUE
For retrieving the value in a cell in a vector, we normally call by its position, such as B[1] for retrieving the value in the first cell of B. It is easy to confuse the negation operator ! with the negative operator -. For example, B[-1] means to retrieve all values except for the one in the first cell. However, B[!1] will return numeric(0). This is because !1 means FALSE. Thus, B[!1] will retrieve nothing from B. Note the NOT operation is not suitable for the character variables. You will get an error message, if you call by !v and v is a character variable.
B[1]
## [1] 1
B[-1]
## [1] 4 5 8 10
B[!1]
## numeric(0)
In R, == is the operator to check whether two entities are equal to each other or not. The outcome of a==b is either T or F, as either a is equal to b or not. To get the remainder of a/b in R, we can use a%%b. Thus, we can check whether the remainder of A/3 is equal to 3 by calling A%%3==3. As the remainder here should be 0, the returned outcome should be FALSE.
3==3
## [1] TRUE
-3==3
## [1] FALSE
A%%3==3
## [1] FALSE
In a number sequence, if we want to separate the numbers to even and odd numbers, how can we do? See the following codes. We firstly declare a number vector C of 7 elements. We can make a TRUE/FALSE judgment for each of the elements being as an even number or not. In this case, TRUE means the even number. We can also retrieve those even numbers directly. Of course, we can get the positions of those even numbers. For practice, you can try to get the odd numbers in this vector.
C<-c(3,8,1,45,20,-85,12)
# Check which is an even number
C%%2==0
## [1] FALSE TRUE FALSE FALSE TRUE FALSE TRUE
# Retrieve even numbers
C[C%%2==0]
## [1] 8 20 12
# Get the positions in which an even number is
which(C%%2==0)
## [1] 2 5 7
Of course, == is not only used to check the equality between two numerical values, but also the equality between two character variables. See the below example. When comparing two vectors, the lengths of them should be equal in order to prevent errors or warnings. If the two vectors are of the same length, the equality check is pairwise.
x<-"Hello"
y<-"hello"
x==y
## [1] FALSE
x1<-c("marry","john","david")
y1<-c("marry","math","english")
x1==y1
## [1] TRUE FALSE FALSE
The inverse of == is !=, meaning not equal. For example, 3!=2 is TRUE. 4!=4 is FALSE. The result of the != operator is the inverse of that of the == operator. Although not often to see, it is valid in R to check TRUE==FALSE or TRUE!=FALSE, as TRUE and FALSE are two pre-defined variables in R.
3!=2
## [1] TRUE
4!=4
## [1] FALSE
x1!=y1
## [1] FALSE TRUE TRUE
TRUE!=FALSE
## [1] TRUE
In R, & and && are the AND operators. What is the AND logical judgment? When two prepositions \(p1\) and \(p2\) are both TRUE, the outcome of \(p1\) AND \(p2\) is TRUE. The outcome is FALSE if anyone of \(p1\) and \(p2\) is FALSE. That is, TRUE AND FALSE must be FALSE and FALSE AND FALSE is FALSE too. Only is TRUE AND TRUE TRUE. See the below codes to learn the AND rule.
TRUE & FALSE
## [1] FALSE
Name<-"John"
Gender<-"Male"
Name=="John" & Gender=="Male"
## [1] TRUE
Name=="Marry" & Gender=="Male"
## [1] FALSE
Name=="John" & Gender=="Female"
## [1] FALSE
Name=="Marry" & Gender=="Female"
## [1] FALSE
A script in R lists the procedure of executing different processes. The first process will be executed first and so on. Sometimes, one or the other process is chosen to be executed, according to the current condition. For example, if…else is one of this kind of function used for controlling the direction of the information flow. The below figure shows an example.
In R, there are many functions relevant to if…else. The easiest is simply if( ). When the argument in parentheses is TRUE, then the processes in the curly brackets will be executed. When it is FALSE, no process in the curly brackets will be executed. See the below codes. Only will the first one be executed, because the variable A has a value of 9 which is in fact larger than 5.
if(A>5){
print("The value is bigger than 5.")
}
## [1] "The value is bigger than 5."
if(A<5){
print("The value is smaller than 5.")
}
The function if( ) is used to only decide whether or not the processes in the if( ) function will be executed. However, we can use if( )…else to decide which process should be executed according to the truth value of the argument in if( ).
A<-4
if(A>5){
print("The value is bigger than 5.")
}else{
print("The value is not bigger than 5.")
}
## [1] "The value is not bigger than 5."
The function if( )…else is used to make a binary choice; either the processes in the curly brackets of if( ) or the processes in the curly brackets of else. In fact, we can make a triple choice with if( )…elseif( )…else. For example, according to his/her age, we can assign the person to one of three classes, teenage, adult, and elder. You can try with several ages in different age intervals and see what you will get.
age<-35
if(age<20){
print("This is a teenager.")
}else if(age>=20 && age <65){
print("This is an adult.")
}else{
print("This is an elder.")
}
## [1] "This is an adult."
A useful function ifelse( ) is worth introducing. For example, if we want to change the value of a variable to either 1 or 0 according to whether of not it is larger than a threshold, we can use this function. In ifelse( ), there are at least three arguments. The first one is the logical judgment, such as >, <, !=, or ==, etc. If the outcome of the logical judgment is TRUE, the value in the second argument will be returned and the value in the third argument will be returned otherwise. Of course, the returned value can be assigned to the original variable.
d<-10;threshold<-7
ifelse(d>threshold,1,0)
## [1] 1
d<-3
ifelse(d>threshold,1,0)
## [1] 0
d<-ifelse(d>threshold,1,0)
d
## [1] 0
Sometimes, when there are many choices, we can also use switch( ). In this function, the first argument must be a character variable. According to the value of it, a suitable process will be executed and its outcome will be returned.
day<-c("Mon","Tue","Wed","Thur","Fri","Sat","Sun")
switch(day[4],
"Mon"="Today is Monday.",
"The"="Today is Tuesday.",
"Wed"="Today is Wednesday.",
"Thur"="Today is Thursday.",
"Fri"="Today is Friday",
"Sat"="Today is Saturday.",
"Sun"="Today is Sunday.")
## [1] "Today is Thursday."
x<-c(3,4,5,6,6,7,8,9)
type="median"
switch(type,
mean=mean(x),
median=median(x),
sd=sd(x))
## [1] 6