Arithmetic Operators with R

Arithmetic Operators 

Arithmetic operators are used to perform mathematical operations on numeric values.

Addition (+)

Syntax : 

a + b

Example : 

a <- 5
b <- 3
result <- a + b  # 8
print(result)

Explanation: Adds the values of a and b, resulting in 8.

Subtraction ()

Syntax: 

a - b

 Example : 

a <- 5
b <- 3
result <- a - b  # 2
print(result)

Explanation: Subtracts b from a, resulting in 2.

Multiplication (*)

Syntax : 

a * b

 Example : 

a <- 5
b <- 3
result <- a * b  # 15
print(result)

Explanation: Multiplies a by b, resulting in 15.

Division (/)

Syntax : 

a / b

Example : 

a <- 5
b <- 3
result <- a / b  # 1.6667
print(result)

Explanation: Divides a by b, resulting in approximately 1.6667.

Exponentiation (^ or **)

Syntax : 

a ^ b
a ** b

 Example : 

a <- 2
b <- 3
result <- a ^ b  # 8
print(result)

Explanation: Raises a to the power of b, resulting in 8.

Modulo (%%)

Syntax: 

a %% b

Example : 

a <- 10
b <- 3
result <- a %% b  # 1
print(result)

Explanation: Computes the remainder of a divided by b, resulting in 1.

Integer Division (%/%)

Syntax : 

a %/% b

Example : 

a <- 10
b <- 3
result <- a %/% b  # 3
print(result)

 Explanation: Computes the integer quotient of a divided by b, resulting in 3.

Boolean Operators

Les opérateurs booléens sont utilisés pour effectuer des opérations logiques sur des valeurs booléennes (TRUE ou FALSE).

Equality (==)

Syntax : 

a == b

Example : 

a <- 5
b <- 3
result <- a == b  # FALSE
print(result)

Explanation: Checks if a is equal to b. The result is FALSE because 5 is not equal to 3.

Inequality (!=)

Syntax : 

a != b

 Example : 

a <- 5
b <- 3
result <- a != b  # TRUE
print(result)

Explanation: Checks if a is not equal to b. The result is TRUE because 5 is not equal to 3.

Less Than (<)

Syntax : 

a < b

Example : 

a <- 5
b <- 10
result <- a < b  # TRUE
print(result)

Explanation: Checks if a is less than b. The result is TRUE because 5 is less than 10.

Greater Than (>)

Syntax : 

a > b

Example : 

a <- 5
b <- 10
result <- a > b  # FALSE
print(result)

Explanation: Checks if a is greater than b. The result is FALSE because 5 is not greater than 10.

Less Than or Equal To (<=)

Syntax: 

a <= b

 Example : 

a <- 5
b <- 5
result <- a <= b  # TRUE
print(result)

Explanation: Checks if a is less than or equal to b. The result is TRUE because 5 is equal to 5.

Greater Than or Equal To (>=)

Syntax : 

a >= b

Example : 

a <- 5
b <- 3
result <- a >= b  # TRUE
print(result)

Explanation: Checks if a is greater than or equal to b. The result is TRUE because 5 is greater than 3.

Logical AND (& and &&)

Syntax :

  • & : Element-wise logical AND.
  • && : Short-circuit logical AND.

Example : 

a <- TRUE
b <- FALSE
result1 <- a & b  # FALSE
result2 <- a && b  # FALSE
print(result1)
print(result2)

Explanation: Both & and && check if both conditions are true. && evaluates only the first element, while & evaluates element-wise.

Logical OR (| and ||)

Syntax :

  • | : Element-wise logical OR.
  • || : Short-circuit logical OR.

Example : 

a <- TRUE
b <- FALSE
result1 <- a | b  # TRUE
result2 <- a || b  # TRUE
print(result1)
print(result2)

Explanation: Both | and || check if at least one condition is true. || evaluates only the first element, while | evaluates element-wise.

Logical NOT (!)

Syntax : 

!a

Example : 

a <- TRUE
result <- !a  # FALSE
print(result)

Explanation: Negates the boolean value of a. If a is TRUE, !a is FALSE.

These operators are fundamental in performing mathematical and logical operations in R. They allow for a wide range of data manipulation and decision-making processes in your code.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print