 R Operators

Operators

Arithmetic Operators

Arithmetic Operators: These unary and binary operators perform arithmetic on numeric or complex vectors

CODE/PROGRAM/EXAMPLE
x <- 48
y <- 7
 55

x - y           #Substraction
 41

x * y           #Multiplication
 336

x / y           #Division
 6.857143

x %% y          #Modulus
 6

x %/% y         #Integer Division
 6

x ^ y           #Exponent
 58706342272

a <- 1:5
b <- 6:10
a
 1 2 3 4 5

b
 6 7 8 9 10

a + b
 7 9 11 13 15

c <- c(1,4)
a + c
 2 6 4 8 6
Warning message:
In a + c : longer object length is not a multiple of shorter object length

If two vectors are of unequal length, the shorter one will be recycled, in order to match the longer vector

Logical Operators

Logical Operators: R's binary and logical operators will look very familiar to programmers. But, we must note that binary operators work on vectors, matrices as well as scalars.

CODE/PROGRAM/EXAMPLE
# Logical Operators
a <- c(TRUE, FALSE, 0, 12)
b <- c(FALSE, TRUE, FALSE, TRUE)
!a                                 #Logical Not
 FALSE TRUE TRUE FALSE

a & b                              #Element wise Logical AND
 FALSE FALSE FALSE TRUE

a && b                             #Logical AND
 FALSE

a | b                              #Element wise Logical OR
 TRUE TRUE FALSE TRUE

a || b                             #Logical OR
 TRUE

Assignment Operators

Assignment Operators: Assign a value to a name. There are three different assignment operators, two of them have leftward and rightward forms.

CODE/PROGRAM/EXAMPLE
#Assignment Operators
A <- 12                 #Leftward Assignment
print(A)
 12

A1 <<- 10               #Leftward Assignment
print(A1)
 10

A2 = 43                 #Leftward Assignment
A2                      #Instead of print(A2), we can just say A2
 43

-123 -> B               #Rightward Assignment
print(B)
 -123

25 ->> B1               #Rightward Assignment
B1
 25

Relational Operators

Relational Operators: These are the binary operators which allow the comparison of values in vectors

CODE/PROGRAM/EXAMPLE
#Relational Operators

x <- 48
y <- 7
x < y                 #Less Than
 FALSE

x > y                 #Greater Than
 TRUE

x <= 50               #Less Than or Equal to
 TRUE

y >= 32               #Greater Than or Equal to
 FALSE

x == y                #Equal to
 FALSE

x != y                #Not Equal to
 TRUE
#Operators_in_r_language #r_logical_operators #arithmetic_operators_in_r #operators_in_r_programming #ternary_operator_r #boolean_operators_in_r_programming #Assignment_Operators_in_r_language

(New page will open, for Comment)

Not yet commented...