R Operators

R operators are symbols that processes computations or operations for variables and values.
There are classified into several categories

  1. Arithmetic Operators - +, -, *, /, %/%, %%, ^
  2. Relational Operators - ==, !=, <, >, <=, >=
  3. Logical Operators - &, |, !

Arithmetic Operators

Used for basic mathematical calculations.

Operator

Description

Example

+

Addition

5 + 3

-

Subtraction

5 - 3

*

Multiplication

5 * 3

/

Division

5 / 3

%/%

Integer Division

5 %/% 3

%%

Modulus (remainder)

5 %% 3

^

Exponentiation

5^3


Example
# Addition
result_add <- 5 + 3  # result_add is 8
result_add
# Subtraction
result_add <- 5 - 3  # result_sub is 2
result_add
# Multiplication
result_mult <- 5 * 3  # result_mult is 15
result_mult
# Division
result_div <- 5 / 3  # result_div is 1.666667
result_div
# Integer Division
result_int_div <- 5 %/% 3  # result_int_div is 1
result_int_div
# Modulus
result_mod <- 5 %% 3  # result_mod is 2
result_mod
# Exponentiation
result_exp <- 5^3  # result_exp is 125
result_exp

Relational Operators

Used for comparisons, and they return a boolean result (1 for true, 0 for false).

Operator

Description

Example

==

Equal to

5 == 3

!=

Not equal to

5 != 3

>

Greater than

5 > 3

<

Less than

5 < 3

>=

Greater than or equal to

5 >= 3

<=

Less than or equal to

5 <= 3


Example
# Equal to
result_equal <- 5 == 3  # result_equal is FALSE

# Not equal to
result_not_equal <- 5 != 3  # result_not_equal is TRUE

# Greater than
result_greater_than <- 5 > 3  # result_greater_than is TRUE

# Less than
result_less_than <- 5 < 3  # result_less_than is FALSE

# Greater than or equal to
result_greater_equal <- 5 >= 3  # result_greater_equal is TRUE

# Less than or equal to
result_less_equal <- 5 <= 3  # result_less_equal is FALSE

Logical Operators

Used to perform logical operations, often in conditional statements.

Operator

Description

Example

&

Logical AND

TRUE & FALSE

|

Logical OR

TRUE | FALSE

!

Logical NOT

!TRUE


Example
# Logical AND
result_and <- TRUE & FALSE  # result_and is FALSE

# Logical OR
result_or <- TRUE | FALSE  # result_or is TRUE

# Logical NOT
result_not <- !TRUE  # result_not is FALSE

Miscellaneous Operators

Some Operators used in R

Operator

Description

Example

%in%

Tests if elements are in a vector

2 %in% c(1, 2, 3)

%>%

Pipe operator (used in magrittr package)

data %>% summary()

%*%

Used for matrix multiplication

matrix1 %*% matrix2

:

Creates a sequence

1:5

?

Provides help for a function or topic

?mean


Example
library(magrittr)

# Example: Applying summary() to a data frame
data <- data.frame(x = rnorm(100), y = rnorm(100))
result <- data %>% summary()

is_in <- 2 %in% c(1, 2, 3)
is_in

?mean

sequence <- 1:5
sequence

matrix1 <- matrix(c(1, 2, 3, 4), nrow = 2)
matrix2 <- matrix(c(5, 6, 7, 8), nrow = 2)

result_matrix <- matrix1 %*% matrix2

print(result_matrix)

Quick Recap - Topics Covered

Operations in R

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK