Site icon R-bloggers

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:
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
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.

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.