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
The data above is entered first using the concatenate function, c, and is assigned to the variable data.a. Next is to insert this data into a matrix using the matrix function, with the following arguments
- 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.
Notice the names of the rows are retained in the output. This, however, does not affect the format of the matrix, and can be overcomed by coding this way
Let’s consider the cbind function,
This is self-explanatory by comparing this to the rbind function.
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.