0% found this document useful (0 votes)
13 views2 pages

R Matrix Operations and Analysis Guide

The document contains a problem set with various tasks related to matrix operations and data manipulation in R. It includes creating identity and zero matrices, generating a matrix from a vector, and performing operations on a dataset called 'Smarket'. Additionally, it covers generating random numbers, matrix multiplication, and finding the inverse of a matrix.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

R Matrix Operations and Analysis Guide

The document contains a problem set with various tasks related to matrix operations and data manipulation in R. It includes creating identity and zero matrices, generating a matrix from a vector, and performing operations on a dataset called 'Smarket'. Additionally, it covers generating random numbers, matrix multiplication, and finding the inverse of a matrix.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROBLEM SET – 2

Debaditya Bhattacharyya (407)


2024-09-08

#Q1.(i).An identity matrix of size 10


A=diag(10);
A;
#Q1.(ii).A zero matrix of size 4*3
B=matrix(0,nrow =4,ncol = 3 );
B;
#Q1.(iii).Use the matrix() function and a colon ":" to create a 3*6 matrix vector M
such that M=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]
M=matrix(c(1:18),nrow = 3,ncol = 6,byrow = TRUE);M;
#[Link] data
[Link]("ISLR")
library(ISLR)
Summary(Smarket)
#Q2.(a) Use dim() function to check number of rows and columns in the dataset
"smarket"
dim(Smarket);
#Q2.(b).pick up the second observation
Smarket[2];
#Q2.(c).pick up all the observations corresponding to Lag3
Smarket[lag(3)];
#Q2.(d).Create a new matrix m by picking up the 3rd,4th,7th,9th and 10th rows of
"smarket"
m<-Smarket[c(3,4,7,9,10),];m;
matrix(m,nrow =5,ncol = 5,byrow = TRUE );
#[Link] a 16 dimensional vector of random numbers from poisson(3)
H=rpois(n=16,lambda = 3);H;
"The 16 dimensional vector is (0,2,4,1,2,4,1,3,1,3,3,3,5,1,9,2)".
#Q3.(a).Create a square matrix of size 4
A=matrix(H,nrow = 4,ncol = 4,byrow = TRUE);A;
"The matrix is [0,2,4,1,2,4,1,3,1,3,3,3,5,1,9,2]”
#Q3.(b).using the vector b=(1,2,0,3),compare the operations A*b and A%*%b and
comment
b=c(1,2,0,3);
A*b;
“The matrix is [0,2,4,1,4,8,2,6,0,0,0,0,15,3,27,6]”
A%*%b;
“The value is [7,19,16,13]”
#Q3.(c).Multiply A with an identity matrix I and compare the operations A*I and A%*
%I.
I=diag(4);I;
“The matrix is [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]”
A*I;
“The matrix is [0,0,0,0,0,4,0,0,0,0,3,0,0,0,0,2]”
A%*%I;
“The value is [0,2,4,1,2,4,1,3,1,3,3,3,5,1,9,2]”
#Q3.(d).If possible find the inverse of A (A is an inverse matrix)
solve(A);
“The inverse matrix is [-0.15,0.30,-0.35,0.15,0.47,0.46,-0.57,-0.07,0.16,-0.12,0.04,0.04,-
0.58,-0.44,0.98,-0.02]”

You might also like