Open In App

Mathematical Computations Using R

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

R Programming Language is widely used for statistical computing and data analysis. One of its core strengths lies in its ability to perform various mathematical computations efficiently. Here, we’ll explore different methods and functions available in R for mathematical operations.

1. Basic Arithmetic Operations

R can handle basic arithmetic operations like addition, subtraction, multiplication, and division just like a calculator.

R
# Addition
a <- 5
b <- 3

# Sum calculation
sum_result <- a + b
print("The sum is:")
print(sum_result)  

# Subtraction
# Difference calculation
difference_result <- a - b
print("The difference is:")
print(difference_result) 

# Multiplication
# Product calculation
product_result <- a * b
print("The product is:")
print(product_result)  

# Division
# Quotient calculation
quotient_result <- a / b
print("The quotient is:")
print(quotient_result) 

Output:

[1] "The sum is:"
[1] 8

[1] "The difference is:"
[1] 2

[1] "The product is:"
[1] 15

[1] "The quotient is:" 
[1] 1.666667

2. Mathematical Computations Using Base R Functions

The base R package provides numerous functions for mathematical computations. Now calculate the mean and standard deviation of a numeric vector.

R
# Create a numeric vector
values <- c(10, 20, 30, 40, 50)

# Calculate mean
mean_value <- mean(values)
print("The mean is:")
print(mean_value)  

# Calculate standard deviation
sd_value <- sd(values)
print("The Standard deviation is:")
print(sd_value) 

Output:

[1] "The mean is:"
[1] 30

[1] "The Standard deviation is:"
[1] 15.81139

3. Mathematical Computations Using apply() Functions

The apply() family of functions applies a function across the rows or columns of a matrix or data frame. So we calculate the sum of each row in a matrix.

R
# Create a matrix
matrix_data <- matrix(1:9, nrow = 3, ncol = 3)
# Apply sum across rows
row_sums <- apply(matrix_data, 1, sum)
print(row_sums)  

Output:

[1] 12 15 18

4. Mathematical Computations Using Custom Functions

We can also create custom apply functions using lapply(), sapply(), mapply(), and tapply(). Here’s an example of applying a custom function to a list.

R
# Create a list
num_list <- list(1:3, 4:6, 7:9)

# Custom function to calculate the mean
custom_mean <- function(x) {
  return(mean(x))
}

# Apply custom function to each element of the list
mean_values <- lapply(num_list, custom_mean)
print(mean_values)  

Output:

[[1]]
[1] 2

[[2]]
[1] 5

[[3]]
[1] 8

5. Mathematical Computations Using fundamental functions

R
# Assigning a negative value to x
x <- -4
# Using abs() to get the absolute value
result <- abs(x)
# Printing the result
print(result) 


# Assigning a positive value to x
x <- 4
# Using sqrt() to get the square root
result <- sqrt(x)
# Printing the result
print(result)  


# Assigning a floating-point value to x
x <- 4.5
# Using ceiling() to round up
result <- ceiling(x)
# Printing the result
print(result) 


# Assigning a floating-point value to x
x <- 4.5
# Using floor() to round down
result <- floor(x)
# Printing the result
print(result)  

Output:

[1] 4

[1] 2

[1] 5

[1] 4

Conclusion

In R, handling everything from basic arithmetic to complex statistical analyses with ease. Through its built-in functions and powerful tools like apply(), R simplifies mathematical computations, making data analysis accessible to all.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads