Inputting Data in Matrix Format

June 6, 2013

(This article was first published on Analysis with R, and kindly contributed to R-bloggers)

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}
\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.

To leave a comment for the author, please follow the link and comment on their blog: Analysis with R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)