Menu

R Programming - (LAB PROGRAMS)



Notice: Undefined index: title in /home/u681245571/domains/studyglance.in/public_html/labprograms/rpdisplay.php on line 89

Aim:

 

Solution :

Week 3

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



Related Content :

R Programming Lab Programs

1) Download and install R-Programming environment and install basic packages using install. packages() command in R.   View Solution

2) Learn all the basics of R-Programming (Data types, Variables, Operators etc,.)   View Solution

3) 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
  View Solution

4) Write R command to
i) Illustrates the usage of Vector subsetting and Matrix subsetting
ii) Write a program to create an array of 3×3 matrixes with 3 rows and 3 columns.
  View Solution

5) Write an R program to draw i) Pie chart ii) 3D Pie Chart, iii) Bar Chart along with chart legend by considering suitable CSV file   View Solution

6) Create a CSV file having Speed and Distance attributes with 1000 records. Write R program to draw
i) Box plots
ii) Histogram
iii) Line Graph
iv) Multiple line graphs
v) Scatter plot
to demonstrate the relation between the cars speed and the distance.
  View Solution

7) Implement different data structures in R (Vectors, Lists, Data Frames)   View Solution

8) Write an R program to read a csv file and analyze the data in the file using EDA (Explorative Data Analysis) techniques.   View Solution

9) Write an R program to illustrate Linear Regression and Multi linear Regression considering suitable CSV file   View Solution