Matrix in R
DATA TYPE:
Graphical Representation of Data Types in R is shown as below:
MATRIX:
Matrix is represented as a two-dimensional structure. It can be created using matrix() function.
CREATING MATRIX:
a<-matrix(data = c(1:16), nrow = 8, ncol = 2, byrow = TRUE,dimnames = NULL)
print(a)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
[6,] 11 12
[7,] 13 14
[8,] 15 16
nrow- number of Rows in Matrix
ncol- number of Column in Matrix
byrow- Either TRUE or FALSE. If FALSE (the default) the matrix is filled by columns, otherwise matrix is filled by rows.
dimnames-Either NULL or List of Length 2. i.e. Rowname and Colum name
#Printing Matrix having byrow=False
a<-matrix(data = c(1:16), nrow = 8, ncol = 2, byrow = FALSE,dimnames =NULL)
print(a)
[,1] [,2]
[1,] 1 9
[2,] 2 10
[3,] 3 11
[4,] 4 12
[5,] 5 13
[6,] 6 14
[7,] 7 15
[8,] 8 16
#Printing Matrix with Row and Column Names
Row<-c("R1","R2","R3","R4","R5","R6","R7","R8")
Col<-c("C1","C2")
a<-matrix(data = c(1:16), nrow = 8, ncol = 2, byrow = TRUE,dimnames = list(Row,Col))
print(a)
C1 C2
R1 1 2
R2 3 4
R3 5 6
R4 7 8
R5 9 10
R6 11 12
R7 13 14
R8 15 16
#Creating Matrix with Single Constant Element
Let us create Matrix of 2 rows and 3 columns having constant element 6.
matrix(6,2,3)
[,1] [,2] [,3]
[1,] 6 6 6
[2,] 6 6 6
#Creating Diagonal Matrix
Diagonal Matrix can be created with Single Constant Element
diag(13,3,3)
[,1] [,2] [,3]
[1,] 13 0 0
[2,] 0 13 0
[3,] 0 0 13
Also, with different values across the diagonal can be created
diag(c(1,2,3),3,3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 2 0
[3,] 0 0 3
#Creating Identity Matrix
Identity matrix is a square matrix having value 1 across the diagonal.
> diag(1,3,3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
#Creating Student Data Matrix
Let us create Matrix of Student marks in various subject
rwname=c('Anu','Annie','Divya','Radha')
clname=c('Java','C++','Accounts','Stats')
Stud_data<-matrix(data=c(81:96),nrow =4,byrow = TRUE,dimnames =list(rwname,clname))
print(Stud_data)
Java C++ Accounts Stats
Anu 81 82 83 84
Annie 85 86 87 88
Divya 89 90 91 92
Radha 93 94 95 96
MATRIX MANIPULATION:
1. MATRIX DIMENSION:
The Dimension of the Matrix can be found by dim() function. In the below output 8 indicates the number of Rows and 2 indicates the number of columns in the matrix "a".
dim(a)
[1] 8 2
2.ADDING COLUMN TO THE MATRIX:
Let us add a new column "Data Mining" to the matrix using cbind() function.
Stud_data<-cbind(Stud_data,c(69:72))
colnames(Stud_data)<-c('Java','C++','Accounts','Stats','Data Mining')
print(Stud_data)
Java C++ Accounts Stats Data Mining
Anu 81 82 83 84 69
Annie 85 86 87 88 70
Divya 89 90 91 92 71
Radha 93 94 95 96 72
A new column "Data Mining" is added.
3.ADDING ROW TO THE MATRIX:
We can add a new row to the matrix using rbind() function.
Stud_data<-rbind(Stud_data,c(89,93,22,55,67))
rownames(Stud_data)<-c('Anu','Annie','Divya','Radha',"Kavi")
print(Stud_data)
Java C++ Accounts Stats Data Mining
Anu 81 82 83 84 69
Annie 85 86 87 88 70
Divya 89 90 91 92 71
Radha 93 94 95 96 72
Kavi 89 93 22 55 67
A new row "Kavi" is added.
4.ACCESSING ELEMENT OF MATRIX
# Accessing Single Element
print(Stud_data[3,3])
[1] 91
print(Stud_data[5,5])
[1] 67
#Print Entire Row
print(Stud_data[2,])
Java C++ Accounts Stats Data Mining
85 86 87 88 70
#Print Entire Column
print(Stud_data[,4])
Anu Annie Divya Radha Kavi
84 88 92 96 55
# Print Entire Matrix
print(Stud_data[,]) OR
Stud_data
Java C++ Accounts Stats Data Mining
Anu 81 82 83 84 69
Annie 85 86 87 88 70
Divya 89 90 91 92 71
Radha 93 94 95 96 72
Kavi 89 93 22 55 67
5.MATRIX COMPUTATION
We can perform addition, subtraction, multiplication, and division only if the matrix is of the same length.
mt1 <- Stud_data
mt2 <- matrix(c(1:25), nrow = 5)
#Addition
mt3<-mt1+mt2
print(mt3)
Java C++ Accounts Stats Data Mining
Anu 82 88 94 100 90
Annie 87 93 99 105 92
Divya 92 98 104 110 94
Radha 97 103 109 115 96
Kavi 94 103 37 75 92
#Subtraction
mt4<-mt1-mt2
print(mt4)
Java C++ Accounts Stats Data Mining
Anu 80 76 72 68 48
Annie 83 79 75 71 48
Divya 86 82 78 74 48
Radha 89 85 81 77 48
Kavi 84 83 7 35 42
#Multiplication
mt5<-mt1*mt2
print(mt5)
Java C++ Accounts Stats Data Mining
Anu 81 492 913 1344 1449
Annie 170 602 1044 1496 1540
Divya 267 720 1183 1656 1633
Radha 372 846 1330 1824 1728
Kavi 445 930 330 1100 1675
#Division
mt6<-mt1/mt2
print(mt6)
Java C++ Accounts Stats Data Mining
Anu 81.00000 13.66667 7.545455 5.250000 3.285714
Annie 42.50000 12.28571 7.250000 5.176471 3.181818
Divya 29.66667 11.25000 7.000000 5.111111 3.086957
Radha 23.25000 10.44444 6.785714 5.052632 3.000000
Kavi 17.80000 9.30000 1.466667 2.750000 2.680000
6. is.matrix()
is.matrix tests if given input is a matrix.
is.matrix(Stud_data)
[1] TRUE
7. Finding Number of Rows
nrow(Stud_data)
[1] 5
8. Finding Number of Columns
ncol(Stud_data)
[1] 5
9. Finding Number of Element
length(Stud_data)
[1] 25
10. Deleting Row from Matrix
Negative Indexing refers to the deletion in Matrix
print(Stud_data)
Java C++ Accounts Stats Data Mining
Anu 81 82 83 84 69
Annie 85 86 87 88 70
Divya 89 90 91 92 71
Radha 93 94 95 96 72
Kavi 89 93 22 55 67
Let us delete 3rd row from the Stud_data
Stud_data<-Stud_data[-3,]
print(Stud_data)
Java C++ Accounts Stats Data Mining
Anu 81 82 83 84 69
Annie 85 86 87 88 70
Radha 93 94 95 96 72
Kavi 89 93 22 55 67
11. Deleting Column from Matrix
Negative Indexing refers to the deletion in Matrix
print(Stud_data)
Java C++ Accounts Stats Data Mining
Anu 81 82 83 84 69
Annie 85 86 87 88 70
Radha 93 94 95 96 72
Kavi 89 93 22 55 67
Let us delete the first column "Java" from the matrix "Stud_data"
Stud_data<-Stud_data[,-1]
print(Stud_data)
C++ Accounts Stats Data Mining
Anu 82 83 84 69
Annie 86 87 88 70
Radha 94 95 96 72
Kavi 93 22 55 67
12. Transpose of Matrix
Transposing means changing Rows as Columns and Columns as Rows. Let us transpose the Stud_data matrix
t(Stud_data)
Anu Annie Radha Kavi
C++ 82 86 94 93
Accounts 83 87 95 22
Stats 84 88 96 55
Data Mining 69 70 72 67
Comments
Post a Comment