R Operator

R Operators

Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com


Table of Contents

  1. Introduction to R Operators

  2. Arithmetic Operators

  3. Relational Operators

  4. Logical Operators

  5. Assignment Operators

  6. Miscellaneous Operators

    • Sequence Operator

    • Membership Operator

    • Identity Operator


1. Introduction to R Operators

In R, operators are symbols that perform specific operations on one or more operands. Operators are used in mathematical calculations, comparisons, and logical evaluations. R supports various types of operators, including arithmetic, relational, logical, and assignment operators.


2. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.

List of Arithmetic Operators:

  • + : Addition

  • - : Subtraction

  • * : Multiplication

  • / : Division

  • ^ : Exponentiation

  • %% : Modulus (remainder of division)

  • %/% : Integer division

Example:

# Arithmetic operations
a <- 10
b <- 3

sum <- a + b  # Addition
diff <- a - b  # Subtraction
prod <- a * b  # Multiplication
quot <- a / b  # Division
power <- a ^ b  # Exponentiation
mod <- a %% b  # Modulus
int_div <- a %/% b  # Integer division

print(sum)  # Output: 13
print(diff)  # Output: 7
print(prod)  # Output: 30
print(quot)  # Output: 3.333
print(power)  # Output: 1000
print(mod)  # Output: 1
print(int_div)  # Output: 3

3. Relational Operators

Relational operators are used to compare two values. The result of a relational operation is a logical value (TRUE or FALSE).

List of Relational Operators:

  • == : Equal to

  • != : Not equal to

  • > : Greater than

  • < : Less than

  • >= : Greater than or equal to

  • <= : Less than or equal to

Example:

# Relational operations
x <- 15
y <- 20

equal <- x == y  # FALSE
not_equal <- x != y  # TRUE
greater <- x > y  # FALSE
less <- x < y  # TRUE
greater_equal <- x >= y  # FALSE
less_equal <- x <= y  # TRUE

print(equal)
print(not_equal)
print(greater)
print(less)
print(greater_equal)
print(less_equal)

4. Logical Operators

Logical operators are used to combine multiple conditions or to negate a condition.

List of Logical Operators:

  • & : Element-wise logical AND

  • | : Element-wise logical OR

  • ! : Logical NOT

  • && : Logical AND (used for single logical comparison)

  • || : Logical OR (used for single logical comparison)

Example:

# Logical operations
p <- TRUE
q <- FALSE

and_result <- p & q  # Element-wise AND: FALSE
or_result <- p | q  # Element-wise OR: TRUE
not_result <- !p  # Logical NOT: FALSE

print(and_result)
print(or_result)
print(not_result)

5. Assignment Operators

Assignment operators are used to assign values to variables. In R, you can use either <-, =, or -> for assignment.

List of Assignment Operators:

  • <- : Assigns value from right to left

  • -> : Assigns value from left to right

  • = : Assigns value from right to left

Example:

# Assignment operations
x <- 25  # Assigning value using <-
25 -> y  # Assigning value using ->
z = 30  # Assigning value using =

print(x)  # Output: 25
print(y)  # Output: 25
print(z)  # Output: 30

6. Miscellaneous Operators

6.1 Sequence Operator

The colon : operator is used to create a sequence of numbers.

Example:

# Sequence operation
seq <- 1:10
print(seq)  # Output: 1 2 3 4 5 6 7 8 9 10

6.2 Membership Operator

The %in% operator is used to check if an element belongs to a vector.

Example:

# Membership operation
vec <- c(1, 2, 3, 4, 5)
is_member <- 3 %in% vec
print(is_member)  # Output: TRUE

6.3 Identity Operator

The identical() function is used to check if two objects are exactly the same.

Example:

# Identity operation
a <- c(1, 2, 3)
b <- c(1, 2, 3)
c <- a

same <- identical(a, b)  # Output: TRUE
identical(a, c)  # Output: TRUE

Conclusion

Operators are essential in R programming, enabling you to perform a wide range of operations, from basic arithmetic to logical comparisons. Understanding these operators is key to manipulating data and writing effective R scripts.

For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.


Last updated