R Matrices
Matrices
Matrix is a two-dimensional data set with columns and rows.
A column is a vertical image of the data, while the line represents the horizontal data.
The matrix can be created with the function of the matrix(). Specify the nrow and ncol parameters for the number of rows and columns:
Example
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
thismatrix
Note: Remember that the function of c() is used to bind objects together.
You can also create a wired matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix
Access Matrix Items
You can access items using [] brackets. The first number "1" in brackets specifies the location of the row, and the second number "2" specifies the location of the column:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[1, 2]
The whole line can be reached if you specify a comma after the number in parentheses:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[2,]
The entire column can be accessed if you specify a comma before the number in parentheses:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
thismatrix[,2]
Access More Than One Row
More than one line can be reached when using the c() function:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[c(1,2),]
Access More Than One Column
More than one column can be accessed using the c() function:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
thismatrix[,
c(1,2)]
Add Rows and Columns
Use the cbind() function to add additional columns to the Matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <-
cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
newmatrix
Note: Cells in a new column must be the same length as the existing matrix.
Use the rbind() function to add additional lines to the Matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape",
"pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)
newmatrix <-
rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))
newmatrix
Note: Cells in a new row must be the same length as the existing matrix.
Remove Rows and Columns
Use the c() function to delete rows and columns in the Matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"),
nrow = 3, ncol =2)
thismatrix <- thismatrix[-c(1), -c(1)]
thismatrix
Check if an Item Exists
To find out if something is in the matrix, use the operator %in%:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
"apple" %in% thismatrix
Amount of Rows and Columns
Use the dim() function to find the number of rows and columns in the Matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
dim(thismatrix)
Matrix Length
Use the length() function to determine the size of the Matrix:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
length(thismatrix)
The number of cells in a matrix is the number of rows multiplied by the number of columns.
In the example above: Size = 2 * 2 = 4.
Loop Through a Matrix
You can enter the Matrix using the for loop. The loop will start on the first row, to the right:
Example
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol
= 2)
for (rows in 1:nrow(thismatrix)) {
for (columns in
1:ncol(thismatrix)) {
print(thismatrix[rows, columns])
}
}
Combine two Matrices
Also, you can use rbind() or cbind() function to combine two or more matrices together:
Example
Matrix1 <- matrix(c("apple", "banana", "cherry",
"grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango",
"pineapple", "watermelon"), nrow = 2, ncol = 2)
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined
© copyright 2021. All rights reserved.