R Program to Perform Arithmetic Operations on Vectors

Introduction

Vectors are a fundamental data structure in R, allowing you to perform element-wise arithmetic operations efficiently. This guide will walk you through writing an R program that performs basic arithmetic operations—addition, subtraction, multiplication, and division—on vectors.

Problem Statement

Create an R program that:

  • Creates two vectors.
  • Performs element-wise addition, subtraction, multiplication, and division on the vectors.
  • Displays the results of each operation.

Example:

  • Input: Two vectors c(1, 2, 3) and c(4, 5, 6)
  • Output: The results of addition, subtraction, multiplication, and division.

Solution Steps

  1. Create Two Vectors: Use the c() function to create two vectors.
  2. Perform Arithmetic Operations: Conduct element-wise operations (addition, subtraction, multiplication, and division) between the vectors.
  3. Display the Results: Use the print() function to display the results of each arithmetic operation.

R Program

# R Program to Perform Arithmetic Operations on Vectors
# Author: https://www.javaguides.net/

# Step 1: Create two vectors
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)

# Step 2: Perform element-wise arithmetic operations

# Addition
addition_result <- vector1 + vector2

# Subtraction
subtraction_result <- vector1 - vector2

# Multiplication
multiplication_result <- vector1 * vector2

# Division
division_result <- vector1 / vector2

# Step 3: Display the results
print(paste("Addition result:", paste(addition_result, collapse = ", ")))
print(paste("Subtraction result:", paste(subtraction_result, collapse = ", ")))
print(paste("Multiplication result:", paste(multiplication_result, collapse = ", ")))
print(paste("Division result:", paste(division_result, collapse = ", ")))

Explanation

Step 1: Create Two Vectors

  • Two vectors vector1 and vector2 are created using the c() function, with elements c(1, 2, 3) and c(4, 5, 6) respectively.

Step 2: Perform Arithmetic Operations

  • Addition: The vectors are added element-wise using the + operator.
  • Subtraction: The vectors are subtracted element-wise using the - operator.
  • Multiplication: The vectors are multiplied element-wise using the * operator.
  • Division: The vectors are divided element-wise using the / operator.

Step 3: Display the Results

  • The results of each arithmetic operation are displayed using the print() function. The paste() function is used to concatenate the vector elements into a comma-separated string for display.

Output Example

Example:

[1] "Addition result: 5, 7, 9"
[1] "Subtraction result: -3, -3, -3"
[1] "Multiplication result: 4, 10, 18"
[1] "Division result: 0.25, 0.4, 0.5"

Conclusion

This R program demonstrates how to perform element-wise arithmetic operations on vectors. It covers essential vector operations, making it a valuable example for beginners learning R programming. Through this program, you can understand how R handles arithmetic with vectors and how to display the results effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top