Inputting Data in Matrix Format
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:
- 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.
Example 1. Consider this matrix, $\left[\begin{array}{ccc}
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.
Now, using the rbind function, the coding is as follows:
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.
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.