# Inputting Data in Matrix Format

June 6, 2013
By

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