...

Artificial Intelligence - R Programming Basic

Back to Course

Lesson Description


Lession - #803 Operators in R


An operator is a symbol that instructs the compiler to perform specific mathematical or logical operations. R programming language has many built-in operators.

R divides the operators in the following groups:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Miscellaneous operators

    Arithmetic operators

    Arithmetic operators are used to perform mathematical calculations. There are various arithmetic operators which are supported by R as follows.

    SNO OPERATOR DESCRIPTION EXAMPLE
    1 + This operator can be used to add two vectors. a <- c (100, 200, 300>

    b<- c (10, 20.5, 30>

    print (a+b>

    Output:

    [1] 110.0 220.5 330.0
    2 - This operator can be used to subtract two vectors. a <- c (100, 200, 300>

    b<- c (10, 20.5, 30>

    print (a-b>

    Output:

    [1] 90.0 179.5 270.0
    3 * This operator can be used to multiply two vectors from each other a <- c (100, 200, 300>

    b<- c (10, 20.5, 30>

    print (a*b>

    Output:

    [1] 1000 4100 9000"
    4 / This operator can be used to divide two vectors from each other. a <- c (100, 200, 300>

    b<- c (10, 20.5, 30>

    print (a/b>

    Output:

    [1] 10.000000 9.756098 10.000000
    5 %% This operator can be used to find the remainder of the first vector with the second. a <- c (100, 200, 300>

    b <- c (10, 20.5, 30>

    print (a%%b>

    Output:

    [1] 0.0 15.5 0.0
    6 %% This operator can be used to find the division of the first vector with the second. a <- c (100, 200, 300>

    b < c (10, 20.5, 30>

    print (a%%b>

    Output:

    [1] 10 9 10
    7 ^ This operator can be used to raise the the first vector to the exponent of second vector./td> a <- c (100, 200, 300>

    b<- c (10, 2.5, 30>

    print (a^b>

    Output:

    [1] 100.0 565685.4 27000000.0

    Relational operators

    Relational operators are used to compare first vector with the second and gives corresponding result. There are various relational operators which are supported by R as follows.

    SNO OPERATOR DESCRIPTION EXAMPLE
    1 > This operator will return TRUE if first vector is greater than second a <- c (10, 20, 30>

    b<- c (20, 25, 60>

    print (a>b>

    Output:

    [1] FALSE TRUE FALSE
    2 < This operator will return TRUE if first vector is lesser than second. a <- c (10, 30, 50>

    b<- c (20, 25, 60>

    print (a< b>

    Output:

    [1] TRUE FALSE TRUE
    3 >= This operator will return TRUE if first vector is greater than or equal to second. a <- c (10, 30, 50>

    b<- c (20, 25, 60>

    print (a>=b>

    Output:

    [1] FALSE TRUE FALSE
    4 <= This operator will return TRUE if first vector is lesser than or equal to second. a <- c (10, 30, 50>

    b<- c (20, 25, 60>

    print (a<=b>

    Output:

    [1] TRUE TRUE TRUE
    5 == This operator will return TRUE if first vector is equal to second. a <- c (10, 30, 50>

    b<- c (20, 25, 60>

    print (a==b>

    Output:

    [1] TRUE FALSE TRUE
    6 != This operator will return TRUE if first vector is not equal to second. a <- c (10, 30, 50>

    b<- c (20, 25, 60>

    print (a!=b>

    Output:

    [1] FALSE TRUE FALSE


    Logical operators

    The Logical operators are used in the program to make a decision on multiple conditions & returns the result as TRUE or FALSE.The logical operator compares the first element of the vector with analogous element of the second vector & returns the respective result.

    >
    SNO OPERATOR DESCRIPTION EXAMPLE
    1 & This operator is known as Logical AND operator, returns TRUE only when both the elements are TRUE. x <- c(10, 0, TRUE, 1+5i>

    y<- c(20, 50, TRUE, 1+5i>

    print(x&y>

    Output:

    [1] TRUE FALSE TRUE TRUE
    2 | This operator is known as Logical OR operator, returns TRUE when any one of the vectors are TRUE. x <- c(10, 0, TRUE, 1+5i>

    y<- c(20, 50, TRUE, 1+5i>

    print(x|y>

    Output:

    [1] TRUE TRUE TRUE TRUE
    3 ! This operator is known as Logical NOT operator, returns logical opposite result of the vectors value. x <- c(10, 0, TRUE, 1+5i>
    print(!x>

    Output:

    [1] FALSE TRUE FALSE FALSE
    4 && This operator is known as Logical AND AND operator, returns TRUE result only when both the elements are TRUE. x <- c(10, 0, TRUE, 1+5i>

    y<- c(20, 50, TRUE, 1+5i>

    print(x&&y>

    Output:

    [1] TRUE
    5 || This operator is known as Logical OR OR operator, returns TRUE result when any one of the elements are TRUE. x <- c(10, 0, TRUE, 1+5i>

    y<- c(20, 50, TRUE, 1+5i>

    print(x||y>

    Output:

    [1] TRUE


    Assignment operators

    The Assignment operators are used to assign new values to the variables in R.

    SNO OPERATOR DESCRIPTION EXAMPLE
    1 <-, =, <<- This operator is called as Left assignment operator. Here variable name is written on left side & value on right side. x <- c (10, 0, TRUE, 2+2i>

    y <<- c (20, 40, TRUE, 2+3i>

    z = c (10, 20, TRUE, 2+3i>

    print (x>

    print (y>

    print (z>

    Output:

    [1] 10+0i 0+0i 1+0i 2+2i
    [1] 20+0i 40+0i 1+0i 2+3i
    [1] 10+0i 20+0i 1+0i 2+3i
    2 ->,->>/td> This operator is called as Right assignment operator. Here variable name is written on right side & value on left side. c(10, 0, TRUE, 2+2i>
    -> x
    c(20, 40, TRUE, 2+3i>
    ->> y
    print (x>

    print (y>

    Output:

    [1] 10+0i 0+0i 1+0i 2+2i [1] 20+0i 40+0i 1+0i 2+3i

    Miscellaneous operators

    Miscellaneous operators in R are used only for a special use & not used for any general math or logical calculations. Following are the miscellaneous operators supported in R

    SNO OPERATOR DESCRIPTION EXAMPLE
    1 : The colon operator is used to create the series of numbers in sequence for a vector. x <- 1:10
    print(x>

    Output:

    [1] 1 2 3 4 5 6 7 8 9 10
    2 %in% This operator can be used, when it is required to identify if an element belongs to vector. x1 <- 5
    x2 <- 15
    y <- 1:10
    print(x1 %in% y>

    print(x2 %in% y>

    Output:

    [1] TRUE [1] FALSE
    3 %*% This operator can be used to multiply a matrix with its transpose. M = matrix( c(1,3,5,2,4,6>
    , nrow = 3,ncol = 3,byrow = TRUE>

    t = M %*% t(M>

    s print(t>

    Output:

    [,1] [,2] [,3]
    [1,] 35 44 35
    [2,] 44 56 44
    [3,] 35 44 35


    Try it here