R Basic Operators
This chapter introduces simple operations in R.
### Assignment
In most languages, the assignment operator is **=**, but R is a mathematical language, so the assignment symbol is similar to the pseudocode in our math textbooks, a left arrow <- :
## Example
a <-123
b <-456
print(a + b)
The execution result of the above code:
579
This assignment symbol is a formal advantage and operational disadvantage of R: formally more suitable for mathematicians, since not all mathematicians are accustomed to using **=** as the assignment operator.
Operationally, the 1+2*3
7
>(1+2)*3
9
>3/4
0.75
>3.4-1.2
2.2
>1-4*0.5^3
0.5
>8/3%%2
8
>8/4%%2
Inf
>3%%2^2
3
>10/3%/%2
10
### Relational Operators
The following table lists the relational operators supported by R. Relational operators compare two vectors, comparing each element of the first vector with the second vector, and return a boolean value.
| Operator | Description |
| --- | --- |
| > | Check if each element of the first vector is greater than the corresponding element of the second vector. |
| = | Check if each element of the first vector is greater than or equal to the corresponding element of the second vector. |
| <= | Check if each element of the first vector is less than or equal to the corresponding element of the second vector. |
## Example
v <-c(2,4,6,9)
tt)
print(v =t)
print(v<=t)
The execution result of the above code:
TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
### Logical Operators
The following table lists the logical operators supported by R, which can be used for numeric, logical, and complex type vectors.
Non-zero numbers (positive or negative) are all TRUE.
Logical operators compare two vectors, comparing each element of the first vector with the second vector, and return a boolean value.
| Operator | Description |
| --- | --- |
| & | Element-wise logical AND operator, combines each element of the first vector with the corresponding element of the second vector. If both elements are TRUE, the result is TRUE, otherwise FALSE. |
| ο½ | Element-wise logical OR operator, combines each element of the first vector with the corresponding element of the second vector. If either element is TRUE, the result is TRUE; if both are FALSE, it returns FALSE. |
| ! | Logical NOT operator, returns the opposite logical value of each element in the vector. If the element is TRUE, it returns FALSE; if the element is FALSE, it returns TRUE. |
| && | Logical AND operator, only judges the first element of the two vectors. If both elements are TRUE, the result is TRUE, otherwise FALSE. |
| || | Logical OR operator, only judges the first element of the two vectors. If either element is TRUE, the result is TRUE; if both are FALSE, it returns FALSE. |
## Example
v <-c(3,1,TRUE,2+3i)
t<-c(4,1,FALSE,2+3i)
print(v&t)
print(v|t)
print(!v)
# &&, || only compare the first element
v <-c(3,0,TRUE,2+2i)
t<-c(1,3,TRUE,2+3i)
print(v&&t)
v <-c(0,0,TRUE,2+2i)
t<-c(0,3,TRUE,2+3i)
print(v||t)
The execution result of the above code:
TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
### Assignment Operators
R variables can be assigned using left, right, or equal operators.
The following table lists the assignment operators supported by R.
| Operator | Description |
| --- | --- |
| <β = < β>> | Assign to the right. |
## Example
# Assign to the left
v1 <-c(3,1,TRUE,"tutorial")
v2 <<-c(3,1,TRUE,"tutorial")
v3 =c(3,1,TRUE,"tutorial")
print(v1)
print(v2)
print(v3)
#
YouTip