What are Matrices?
Matrices are the another type of R object which arranges data in 2 dimensional layout. They are like mathematical matrix with a defined set of row and column. These matrices can store, number, character, Boolean, integer or complex.MATRIX CREATION METHOD
## Method 1. Use function matrix. This function needs three inputs. First one is data which can be passed in the form of vector.Second one is number of rows and third is number of columns.m<-matrix(c(1,2,3,4),nrow=2, ncol=2)
m<-matrix(1:4 ,nrow=2, ncol=2)
m<-matrix(seq(1,4,by=1),nrow=2, ncol=2)
v<-c(1,2,3,4)
m<-matrix(v ,nrow=2, ncol=2)
[,1] [,2]
[1,] 1 3
[2,] 2 4
It will create a vector of 2 rows and 2 columns
## Method 2.Change the dimensions of vector. A vector is one dimensional set of data. If we change the dimensions of vector it can take form of matrix.
v<-c(1,2,3,4,5,6)
dim(v)<-c(2,3)
## Method 3. Use cbind(), rbind() function.
v1<-c(1,2,3,4)
v2<-c(5,6,7,8)
Now these two vectors can be binded horizontally or vertically for form a matrix.
cbind(v1,v2) rbind(v1,v2)
[,1] [,2] [,1] [,2] [,3] [,4]
[1,] 1 5 [1,] 1 2 3 4
[2,] 2 6 [2,] 5 6 7 8
[3,] 3 7
[4,] 4 8
## Method 4. create a blank matrix and update the matrix when needed.
m<-matrix(,nrow=2,ncol=2)
m
[1,] [2,]
[,1] NA NA
[,2] NA NA
Now we can update the matrix
m[1,1] = 1, m[1,2]=2,
m[2,1] =3, m[2,2]=3
MATRIX ATTRIBUTES
## dim function give the detail of dimensions of matrixm<-m(1:16, 4 4)
dim(m)<-c(8,2) ## update dimensions of matrix
dim(m) ## Return dimension of matrix
[1] 4 4
colnames(m)<-c("Q1","Q2","Q3","Q4") ##update column name of matrix m
colnames(m) ##Return column name of matrix m
rownames(m)>-c(1,2,3,4) ##update row names of matrix m
rownames(m) ##update col names of matrix m
ACCESSING AND MODIFYING MATRIX
## An element of matrix can be modified by its row number and column numberm[1,1] ## return the first element of first row of matrix m
m[1,] ## return all elements of first row of matrix
m[,1] ## return all elements of first column of matrix
m[2,3:4] ## return third and fourth column of second row
m[2:3,1] ##return the second and third element of first column.
m[,] ## return all the elements of the matrix.
MATRIX OPERATIONS
## Addition of matrix
m1<-matrix(1;4,2,2)
m2<-matirx(5:8,2,2)
m1+m2 ## addition of matrix
m2-m1 ## subtraction of matrix
m1*m2 ## product of matrix
m2/m1 ##division of matrix
m1%*%m2 ##matrix multiplication
t(m) ## transpose of matrix
diag(m) ##diagonal of matrix
eigen(m) ## eigen value and eigen vectors of matrix
det(m) ## determinant of matrix
tr(m) ## trace of matrix
SOLVING EQUATIONS BY MATRIX
x+2y=7
3x+y=11
##create a matrix of coefficients of x,y
a=matrix(c(1,3,2,1),2,2)
b=matrix(c(7,11),2,1)
solve(a,b)
When b is not passed the solve(a) will return the inverse of a.
QUESTIONS
## Q1 Give the general expression to create a matrix in R.
The general expression to create a matrix in R is - matrix(data, nrow, ncol, byrow, dimnames)
## Q2 How do you access the element in the 2nd column and 4th row of a matrix named M?
The expression M[4,2] gives the element at 4th row and 2nd column.
## Q3 The sales percentage of two branches for 4 weeks is as follows ( Week start from Monday and end on Sunday).
1)40,45,34,67,56,87,45,23,45,27,37,87,98,45,25,35,54,56,76,84,65,35,56,45,67,67,77,87
2)34,37,39,41,45,49,51,46,45,49,52,55,58,60,67,55,54,58,65,69,70,74,75,65,64,68,69,74
3.1. Find Average, max, min sales of both stores?
3.2 which day was best and worst of both stores?
3.3 Week average, week min, week max sales of both store?
3.4 Average sales of both the stores in the form of matrix?
3.5 Which store was performing better for each daty. Answer should be in the form of matrix value 1 or 2?
The general expression to create a matrix in R is - matrix(data, nrow, ncol, byrow, dimnames)
## Q2 How do you access the element in the 2nd column and 4th row of a matrix named M?
The expression M[4,2] gives the element at 4th row and 2nd column.
## Q3 The sales percentage of two branches for 4 weeks is as follows ( Week start from Monday and end on Sunday).
1)40,45,34,67,56,87,45,23,45,27,37,87,98,45,25,35,54,56,76,84,65,35,56,45,67,67,77,87
2)34,37,39,41,45,49,51,46,45,49,52,55,58,60,67,55,54,58,65,69,70,74,75,65,64,68,69,74
3.1. Find Average, max, min sales of both stores?
3.2 which day was best and worst of both stores?
3.3 Week average, week min, week max sales of both store?
3.4 Average sales of both the stores in the form of matrix?
3.5 Which store was performing better for each daty. Answer should be in the form of matrix value 1 or 2?
No comments:
Post a Comment