R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Operators in R

Operators in R are symbols that perform specific operations on one or more operands. R has a wide range of operators to perform tasks ranging from arithmetic to logical comparisons to member selection.

In this tutorial, we will cover:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Miscellaneous Operators

1. Arithmetic Operators

These operators are used to perform arithmetic calculations.

OperatorDescriptionExampleResult
+Addition3 + 47
-Subtraction7 - 43
*Multiplication3 * 412
/Division8 / 24
%%Modulus5 %% 32
%/%Integer Division5 %/% 31
^Exponentiation3^29

2. Relational Operators

These operators are used to compare two values.

OperatorDescriptionExampleResult
==Equal to5 == 3FALSE
!=Not equal to5 != 3TRUE
>Greater than5 > 3TRUE
<Less than5 < 3FALSE
>=Greater than or equal to5 >= 5TRUE
<=Less than or equal to5 <= 7TRUE

3. Logical Operators

Used to perform logical operations.

OperatorDescriptionExampleResult
&Element-wise ANDc(TRUE, FALSE) & c(TRUE, TRUE)c(TRUE, FALSE)
``Element-wise ORc(TRUE, FALSE) | c(FALSE, TRUE)
!NOT!c(TRUE, FALSE)c(FALSE, TRUE)
&&And (returns single TRUE or FALSE)TRUE && FALSEFALSE
||Or (returns single TRUE or FALSE)TRUE || FALSETRUE

4. Assignment Operators

Used to assign values.

OperatorDescriptionExample
<-Assigns value on the right to the leftx <- 5
->Assigns value on the left to the right5 -> x
=Assigns value (mostly used in function arguments)f(x = 5)

5. Miscellaneous Operators

OperatorDescriptionExampleResult
:Creates a sequence3:63 4 5 6
%in%Checks if left is a member of right3 %in% c(1, 2, 3, 4)TRUE

Conclusion

Operators in R are powerful and allow for flexible manipulation and evaluation of data. Understanding them is crucial as they form the foundation for almost every operation and evaluation in R programming.

  1. Arithmetic operators in R programming:

    • Arithmetic operators perform basic mathematical operations.
    # Example
    a <- 5
    b <- 3
    
    addition_result <- a + b
    subtraction_result <- a - b
    multiplication_result <- a * b
    division_result <- a / b
    
  2. Comparison operators in R:

    • Comparison operators compare values and return logical results.
    # Example
    x <- 10
    y <- 15
    
    greater_than <- x > y
    less_than_equal <- x <= y
    not_equal <- x != y
    
  3. Logical operators in R:

    • Logical operators perform logical operations on Boolean values.
    # Example
    p <- TRUE
    q <- FALSE
    
    and_result <- p & q
    or_result <- p | q
    not_result <- !p
    
  4. Assignment operators in R:

    • Assignment operators assign values to variables.
    # Example
    x <- 10
    y <<- 20  # Assign in global environment
    z <- c(1, 2, 3)
    
  5. Bitwise operators in R:

    • Bitwise operators perform operations on binary representations of integers.
    # Example
    a <- 5
    b <- 3
    
    bitwise_and <- a & b
    bitwise_or <- a | b
    bitwise_xor <- xor(a, b)
    
  6. Special operators in R (e.g., %in%, %*%):

    • Special operators serve specific purposes, like checking membership or performing matrix multiplication.
    # Example
    vector_check <- 3 %in% c(1, 2, 3, 4)
    matrix_multiply <- matrix(1:4, nrow = 2) %*% matrix(1:4, ncol = 2)
    
  7. Vectorized operations and operators in R:

    • R supports vectorized operations, allowing operations on entire vectors.
    # Example
    vector_a <- c(1, 2, 3)
    vector_b <- c(4, 5, 6)
    
    elementwise_sum <- vector_a + vector_b
    
  8. Operator precedence in R:

    • Operator precedence determines the order in which operators are evaluated.
    # Example
    result <- 2 + 3 * 4
    
  9. Overloading operators in R programming:

    • Operator overloading allows you to define custom behavior for operators with your own classes.
    # Example (S3 method for overloading +)
    setMethod("+", signature = c("MyClass", "MyClass"), function(e1, e2) {
      # Custom addition logic
    })