Operators in R
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides following types of operators. Types of Operators
We have the following types of operators in R programming −
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators
In [1]:
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
print(paste("Sum:",a+b))
print(paste("Sub:",a-b))
print(paste("Mul:",a*b))
print(paste("Div:",a/b))
print(paste("Exponent:",a^b))
print(paste("Modulus:",a%%b))
print(paste("Integer Division:",a%/%b))
Enter First No: 5 Enter Second No: 2 [1] "Sum: 7" [1] "Sub: 3" [1] "Mul: 10" [1] "Div: 2.5" [1] "Exponent: 25" [1] "Modulus: 1" [1] "Integer Division: 2"
In [2]:
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
c=a<b
print(c)
Enter First No: 4 Enter Second No: 2 [1] FALSE
In [3]:
x <- 10
print(x)
[1] 10
In [4]:
x = 10
print(x)
[1] 10
In [5]:
x <<-10
print(x)
[1] 10
In [6]:
10 -> x
print(x)
[1] 10
In [7]:
10 ->> x
print(x)
[1] 10
R Logical Operators
| Operator | Description | | — | — | | ! | Logical NOT | | & | Element-wise logical AND | | && | Logical AND | | | | Element-wise logical OR | | || | Logical OR |
- Operators & and | perform element-wise operation producing result having length of the longer operand.
- But && and || examines only the first element of the operands resulting into a single length logical vector.
- Zero is considered FALSE and non-zero numbers are taken as TRUE.
In [3]:
a=as.integer(readline(prompt="Enter First No: "))
b=as.integer(readline(prompt="Enter Second No: "))
print(a&&b)
print(a||b)
print(!b)
Enter First No: 4 Enter Second No: 2 [1] TRUE [1] TRUE [1] FALSE
In [9]:
a=c(10,20,30,40,50)
print(a)
print(a[1])
[1] 10 20 30 40 50 [1] 10
In [10]:
a=c(10,20,30,40,0)
b=c(0,74,35,-56,24)
print(a&b)
print(a|b)
[1] FALSE TRUE TRUE TRUE FALSE [1] TRUE TRUE TRUE TRUE TRUE
In [11]:
a=c(10,20,30,40,0)
b=c(0,74,35,-56,24)
print(a&&b)
print(a||b)
[1] FALSE [1] TRUE
In [12]:
v = 2:8
print(v)
[1] 2 3 4 5 6 7 8
In [13]:
v1 = 8
v2 = 12
t = 1:10
print(v1 %in% t)
print(v2 %in% t)
[1] TRUE [1] FALSE