๐Ÿ“Š
B.TECH III YEAR โ€ข I SEMESTER โ€ข R22

R Programming - Lab Manual

R Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š I Semester ๐Ÿ“Š R Programming ๐Ÿงช Lab Program
Selected Program

Week 3

โ† All R Programs

Aim:

☛   Write R command to
i) Illustrate summation, subtraction, multiplication, and division operations on vectors using vectors.
ii) Enumerate multiplication and division operations between matrices and vectors in R console

Solution :


i) Illustrate summation, subtraction, multiplication, and division operations on vectors using vectors.


In R, you can perform element-wise summation, subtraction, multiplication, and division operations directly on vectors using standard arithmetic operators.

Program:

Filename: vectoroperations.R

# Define two numeric vectors
vector1 <- c(10, 20, 30, 40)
vector2 <- c(2, 4, 6, 8)

# Perform operations and display results using cat
cat("Vector 1: ", vector1, "\n")
cat("Vector 2: ", vector2, "\n\n")

# Summation
sum_result <- vector1 + vector2
cat("Summation: ", sum_result, "\n")

# Subtraction
sub_result <- vector1 - vector2
cat("Subtraction: ", sub_result, "\n")

# Multiplication
mul_result <- vector1 * vector2
cat("Multiplication: ", mul_result, "\n")

# Division
div_result <- vector1 / vector2
cat("Division: ", div_result, "\n")

Output:

Vector 1:  10 20 30 40 
Vector 2:  2 4 6 8 

Summation:  12 24 36 48 
Subtraction:  8 16 24 32 
Multiplication:  20 80 180 320 
Division:  5 5 5 5 


ii) Enumerate multiplication and division operations between matrices and vectors in R console.

Program:

Filename: vectoroperations.R

# Define matrix and vectors
mat <- matrix(c(2, 4, 6, 8, 10, 12), nrow = 3, ncol = 2)
vec_col <- c(1, 2)   # Length = number of columns
vec_row <- c(1, 2, 3)  # Length = number of rows
vec_mul <- c(1, 2)   # For matrix multiplication

cat("Matrix (3x2):\n")
print(mat)
cat("\nVector for column-wise ops:\n")
print(vec_col)
cat("\nVector for row-wise ops:\n")
print(vec_row)

# ---------------------------
# 1. Element-wise Multiplication (Column-wise)
cat("\n1. Element-wise Multiplication (Column-wise): \n")
print(mat * vec_col)

# 2. Element-wise Multiplication (Row-wise)
cat("\n2. Element-wise Multiplication (Row-wise): \n")
print(t(t(mat) * vec_row))

# 3. Element-wise Division (Column-wise)
cat("\n3. Element-wise Division (Column-wise): \n")
print(mat / vec_col)

# 4. Element-wise Division (Row-wise)
cat("\n4. Element-wise Division (Row-wise): \n")
print(t(t(mat) / vec_row))

# 5. Matrix Multiplication (%*%)
cat("\n5. Matrix Multiplication :\n")
print(mat %*% vec_mul)

	

Output:

Matrix (3x2):
     [,1] [,2]
[1,]    2    8
[2,]    4   10
[3,]    6   12

Vector for column-wise ops:
[1] 1 2

Vector for row-wise ops:
[1] 1 2 3

1. Element-wise Multiplication (Column-wise): 
     [,1] [,2]
[1,]    2   16
[2,]    8   10
[3,]    6   24

2. Element-wise Multiplication (Row-wise): 
     [,1] [,2]
[1,]    2   16
[2,]   12   10
[3,]   12   36

3. Element-wise Division (Column-wise): 
     [,1] [,2]
[1,]    2    4
[2,]    2   10
[3,]    6    6

4. Element-wise Division (Row-wise): 
         [,1] [,2]
[1,] 2.000000    4
[2,] 1.333333   10
[3,] 3.000000    4

5. Matrix Multiplication :
     [,1]
[1,]   18
[2,]   24
[3,]   30


๐Ÿ’ป Run This R Program

Week 3
The selected R program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your R program runs inside the embedded R environment; Study Glance does not execute R code directly on your web server. Programs that draw charts (pie, bar, box plots, histograms), read a CSV file from your own computer or install packages may not behave the same way in an online sandbox. Run those in RStudio or the R console.
โ† Back to R Programming Lab Programs ๐Ÿ“š All Lab Manuals