Web service
20 Best API Testing Tools in 2021: REST & SOAP Web Services
{loadposition top-ads-automation-testing-tools} An API or Application Programming Interface is a...
A matrix function in R is a 2-dimensional array that has m number of rows and n number of columns. In other words, matrix in R programming is a combination of two or more vectors with the same data type.
Note: It is possible to create more than two dimensions arrays with matrix function in R.
We can create a matrix with the function matrix(). Following is a function to create a matrix in R which takes three arguments:
matrix(data, nrow, ncol, byrow = FALSE)
Arguments:
Let's construct two 5x2 matrix with a sequence of number from 1 to 10, one with byrow = TRUE and one with byrow = FALSE to see the difference.
# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow = TRUE matrix_a <-matrix(1:10, byrow = TRUE, nrow = 5) matrix_a
Output:
Now, let's print dimension of the matrix in R with dim(). The syntax to print matrix in R using dim() is:
# Print dimension of the matrix with dim() dim(matrix_a)
Output:
## [1] 5 2
# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow = FALSE matrix_b <-matrix(1:10, byrow = FALSE, nrow = 5) matrix_b
Output:
Again, print the dimension of the matrix using dim(). Below is a syntax of R print matrix dimension:
# Print dimension of the matrix with dim() dim(matrix_b)
Output:
## [1] 5 2
Note: Using command matrix_b <-matrix(1:10, byrow = FALSE, ncol = 2) will have same effect as above.
You can also create a 4x3 matrix using ncol. R will create 3 columns and fill the row from top to bottom. Check an example
matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3) matrix_c
Output:
## [,1] [,2] [,3] ## [1,] 1 5 9 ## [2,] 2 6 10 ## [3,] 3 7 11 ## [4,] 4 8 12
Example:
dim(matrix_c)
Output:
## [1] 4 3
You can add column to matrix R with the cbind() command. cbind() means column binding. cbind() can concatenate as many matrix or columns as specified. For example, our previous example created a 5x2 matrix. We concatenate a third column and verify the dimension is 5x3
Example:
# concatenate c(1:5) to the matrix_a matrix_a1 <- cbind(matrix_a, c(1:5)) # Check the dimension dim(matrix_a1)
Output:
## [1] 5 3
Example:
matrix_a1
Output
## [,1] [,2] [,3] ## [1,] 1 2 1 ## [2,] 3 4 2 ## [3,] 5 6 3 ## [4,] 7 8 4 ## [5,] 9 10 5
Example:
We can also add column to matrix R, more than one time. Let's see the next sequence of number to the matrix_a2 matrix. The dimension of new matrices in R will be 4x6 with number from 1 to 24.
matrix_a2 <-matrix(13:24, byrow = FALSE, ncol = 3)
Output:
## [,1] [,2] [,3] ## [1,] 13 17 21 ## [2,] 14 18 22 ## [3,] 15 19 23 ## [4,] 16 20 24
Example:
matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3) matrix_d <- cbind(matrix_a2, matrix_c) dim(matrix_d)
Output:
## [1] 4 6
NOTE: The number of rows of matrices in R should be equal for cbind work
cbind()concatenate columns, rbind() appends rows. Let's add one row to our matrix_c matrix and verify the dimension is 5x3
matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3) # Create a vector of 3 columns add_row <- c(1:3) # Append to the matrix matrix_c <- rbind(matrix_c, add_row) # Check the dimension dim(matrix_c)
Output:
## [1] 5 3
We can select elements one or many elements from a matrix in R programming by using the square brackets [ ]. This is where slicing comes into the picture.
For example:
Here is the output you get for the above codes
{loadposition top-ads-automation-testing-tools} An API or Application Programming Interface is a...
Training Summary SoapUI is the market leader in API Testing Tool. You can do functional, load,...
Training Summary Apache Hive helps with querying and managing large datasets real fast. It is an...
What are Decision Trees? Decision Trees are versatile Machine Learning algorithm that can perform...
MP3 Downloaders are applications that enable you to download your favourite songs like classics,...
What is OLTP? OLTP is an operational system that supports transaction-oriented applications in a...