R Tutorial

# Arithmetic Operations in R Programming

Basic arithmetic operations in R encompass fundamental mathematical computations that can be applied to numeric data. These operations include addition, subtraction, multiplication, division, and exponentiation. R employs these operations for scalar values, vectors, matrices, and more complex data structures.

The addition operator is +. It is used to add two or more numbers together. For example, the following code adds the numbers 1 and 2 together and stores the result in the variable sum:

sum <- 1 + 2 #3

## Subtraction

The subtraction operator is -. It is used to subtract two numbers from each other. For example, the following code subtracts the number 2 from the number 5 and stores the result in the variable difference:

difference <- 5 - 2 #3

## Multiplication

The multiplication operator is *. It is used to multiply two or more numbers together. For example, the following code multiplies the numbers 2 and 3 together and stores the result in the variable product:

product <- 2 * 3 #6

## Division

The division operator is /. It is used to divide one number by another. For example, the following code divides the number 10 by the number 2 and stores the result in the variable quotient:

quotient <- 10 / 2 #5

## Exponentiation

The exponentiation operator is ^ . It is used to raise one number to the power of another number. For example, the following code raises the number 2 to the power of 3 and stores the result in the variable power:

power <- 2^3 #8

## Modulus

The modulus operator is %%. is used to find the remainder when one number is divided by another number. For example, the following code finds the remainder when the number 10 is divided by the number 3 and stores the result in the variable remainder:

remainder <- 10 %% 3 #1

## Vector arithmetic

The basic arithmetic operations that can be performed on vectors are the same as the basic arithmetic operations that can be performed on numbers. However, when you perform arithmetic operations on vectors, the operations are performed element-wise. This means that the operation is performed on each element of the vector, and the result is a new vector with the same length as the original vectors.

vector_a <- c(1, 2, 3, 4, 5) vector_b <- c(6, 7, 8, 9, 10) vector_sum <- vector_a + vector_b #7 9 11 13 15

## Matrix arithmetic

Matrix arithmetic is a powerful tool that can be used to perform calculations on matrices in R. Matrices are two-dimensional arrays of data, and they can contain any type of data, such as numbers, strings, logical values, or even other matrices.

matrix_a <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) matrix_b <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3) matrix_sum <- matrix_a + matrix_b #Output: [,1] [,2] [,3] [1,] 8 10 12 [2,] 13 14 15

Additionally, R supports other arithmetic functions, such as calculating square roots using sqrt(), absolute values using abs(), logarithms using log(), and exponentials using exp().

sqrt_result <- sqrt(25) # 5 absolute_value <- abs(-10) # 10 log_result <- log(100) # 4.60517 exp_result <- exp(2) # 7.389056
Points to remember:
1. The order of operations in R is the same as the order of operations in mathematics.
2. Arithmetic operations are performed from left to right, unless there are parentheses to indicate otherwise.
3. Parentheses can be used to group expressions together and change the order of operations.
4. It is a good practice to use parentheses to make your code more readable and easier to debug.

### Conclusion

Basic arithmetic operations in R encompass addition, subtraction, multiplication, division, and exponentiation, which can be applied to numeric values, vectors, and matrices. These operations allow for fundamental mathematical computations and data manipulation within the language, enabling versatile numerical analyses.