Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Course: Basic Business Analytics Using R

Specialization: MBA- BA
Name: Aditya Nitnaware
UID: 2023-0909-0001-0005

Practical No. 02
Title: Implementation of various operations on matrix and data-frame using R.

Objective: To provide the knowledge and develop the skills to apply various operations on data
structures in R.

Q.1) Create the following matrices using

a) Find A + B, A-B and A*B

b) Extract the second row of matrix A.

c) Extract the third column of matrix B.

d) Extract the first and third element of second row of matrix A.

e) Replace the second element of second row of matrix B by 50

Answer

# Creating a matrix

A = matrix(c(10,24,-10,15,15,25,24,18,40),nrow = 3,ncol = 3)

B = matrix(c(45,30,10,35,-25,40,24,20,50),nrow = 3,ncol = 3)

#a) Find A + B , A-B and A*B

A+B

A-B

A*B

#b) Extract the second row of matrix A

A[2,]

#c) Extract the third column of matrix B.


B[,3]

#d) Extract the first and third element of second row of matrix A.

A[c(1,3),2]

#e) replace the second element of second row of matrix B by 50.

B[B==B[2,2]]<-50

B
Answer

NameofEmployee <- c('Kamlesh', 'Sangita', 'Rahul', 'Dinesh')

Department <- c('Research', 'Sale', 'Finance', 'Admin')

Designation <- c('Research Assistant', 'Sale Manager', 'Risk Analyst','HR Assistant')

DF1 <- data.frame(NameofEmployee, Department, Designation)

DF1

Age <- c(45,34,42,38)

Salary <- c(90000,80000,95000,80000)

DF2 <- data.frame(Age, Salary)

DF2

#a) Join the both data frames to create a single data frame.

DF3 <- cbind(DF1,DF2)


DF3

#b) Extract all the details about Rahul.

DF3[3,]

#c) Extract the data which display employee names with their salary.

DF3[,c(1,5)]

#d) Find the average salary of the employee

mean(DF3$Salary)

You might also like