Inputting Data in Matrix Format
[This article was first published on Analysis with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Matrix in R is formed using matrix, rbind, or cbind function. These functions have the following descriptions:Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
- matrix – used to transform a concatenated data into matrix form of compatible dimensions.
- rbind – short for row bind, that binds a concatenated data points of same sizes by row.
- cbind – short for column bind, that binds a concatenated data points of same sizes by column.
3&4&5\\
2&1&3\\
6&5&4
\end{array}\right]$. Using the matrix function above, we have
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data.a <- c(3,4,5,2,1,3,6,5,4) | |
matrix.a <- matrix(data.a, nrow = 3, ncol = 3, byrow = TRUE) | |
matrix.a | |
[,1] [,2] [,3] | |
[1,] 3 4 5 | |
[2,] 2 1 3 | |
[3,] 6 5 4 |
- data.a – the data
- nrow – the number of rows
- ncol – the number of columns
- byrow – the orientation of how data is inserted into the matrix. If TRUE, then it is by row, otherwise, by column.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
row1 <- c(3,4,5) | |
row2 <- c(2,1,3) | |
row3 <- c(6,5,4) | |
matrix.b <- rbind(row1, row2, row3) | |
matrix.b | |
[,1] [,2] [,3] | |
row1 3 4 5 | |
row2 2 1 3 | |
row3 6 5 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix.c <- rbind(c(3,4,5), c(2,1,3), c(6,5,4)) | |
matrix.c | |
[,1] [,2] [,3] | |
[1,] 3 4 5 | |
[2,] 2 1 3 | |
[3,] 6 5 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix.d <- cbind(c(3,2,6),c(4,1,5),c(5,3,4)) | |
matrix.d | |
[,1] [,2] [,3] | |
[1,] 3 4 5 | |
[2,] 2 1 3 | |
[3,] 6 5 4 |
To leave a comment for the author, please follow the link and comment on their blog: Analysis with R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.