How to create a matrix in R

A matrix in R is a two-dimensional rectangular data set and thus it can be created using vector input to the matrix function. It is similar to vector but additionally contains the dimension attribute.

List of most common functions for creating matrix:

Function                           Description                                                   Example

cbind(a, b, c)           Combine vectors as columns in a matrix        cbind(1:5, 6:10, 11:15)

rbind(a, b, c)          Combine vectors as rows in a matrix               rbind(1:5, 6:10, 11:15)

matrix(x, nrow,      Create a matrix from a vector x                         matrix(x = 1:12, nrow = 3, ncol = 4)

 ncol, byrow)

We create a new matrix with usage of matrix() method. It can take one or more arguments. One that is necessary is list of values which will be contained in matrix, and other two that are optional are nrow and ncol which represent number of rows and columns.

We can specify names of rows and column, but we must provide argument about dimensions of a matrix:

              x <- matrix(1:9, ncol= 3, dimnames = list(c(“X”,”Y”,”Z”), c(“A”,”B”,”C”)))

x

  A B C

X 1 4 7

Y 2 5 8

Z 3 6 9

Matrices can either contain numbers or character vectors, not both!. If you try to create a matrix with both numbers and characters, it will turn all the numbers into characters:

cbind(c(1, 2, 3, 4, 5),

      c(“a”, “b”, “c”, “d”, “e”))

      [,1] [,2]

 [1,] “1”  “a”

 [2,] “2”  “b”

 [3,] “3”  “c”

 [4,] “4”  “d”

 [5,] “5”  “e”

The matrix() function creates a matrix form a single vector of data. The function has 4 main inputs: data – a vector of data, nrow – the number of rows you want in the matrix, and ncol – the number of columns you want in the matrix, and byrow – a logical value indicating whether you want to fill the matrix by rows.

Check out the help menu for the matrix function (?matrix) to see some additional inputs. Let’s use the matrix() function to re-create a matrix containing the values from 1 to 10.

Create a matrix of the integers 1:10,  with 5 rows and 2 columns:

matrix(data = 1:10,

       nrow = 5,

       ncol = 2)

      [,1] [,2]

 [1,]    1    6

 [2,]    2    7

 [3,]    3    8

 [4,]    4    9

 [5,]    5   10

Now with 2 rows and 5 columns:

matrix(data = 1:10,

       nrow = 2,

       ncol = 5)

      [,1] [,2] [,3] [,4] [,5]

 [1,]    1    3    5    7    9

 [2,]    2    4    6    8   10

Now with 2 rows and 5 columns, but fill by row instead of columns:

matrix(data = 1:10,

       nrow = 2,

       ncol = 5,

       byrow = TRUE)

      [,1] [,2] [,3] [,4] [,5]

 [1,]    1    2    3    4    5

 [2,]    6    7    8    9   10

We can change names of rows and columns which are already defined:

x <- matrix(1:9, ncol= 3, dimnames = list(c(“X”,”Y”,”Z”), c(“A”,”B”,”C”)))

x

  A B C

X 1 4 7

Y 2 5 8

Z 3 6 9

              colnames(x) <- c(“Col1″,”Col2″,”Col3”)

x

  Col1 Col2 Col3

X    1    4    7

Y    2    5    8

Z    3    6    9

To change row names, use rownames(x) in same way.

We can also use cbind() and rbind() to concatenate rows and columns in creating a matrix:

              x <- cbind(c(1,2,3), c(7,8,9))

x

     [,1] [,2]

[1,]    1    7

[2,]    2    8

[3,]    3    9

rbind() will put 1 2 3 on first row and 7 8 9 on  second row:

x <- rbind(c(1,2,3), c(7,8,9))

x

     [,1] [,2] [,3]

[1,]    1    2    3

[2,]    7    8    9

cbind() and rbind() both create matrices by combining several vectors of the same length. cbind() combines vectors as columns, while rbind() combines them as rows.

Let’s use these functions to create a matrix with the numbers 1 through 30. First, we’ll create three vectors of length 5, then we’ll combine them into one matrix. As you will see, the cbind() function will combine the vectors as columns in the final matrix, while the rbind() function will combine them as rows.

x <- 1:5

y <- 6:10

z <- 11:15

Create a matrix where x, y and z are columns:

cbind(x, y, z)

      x  y  z

 [1,] 1  6 11

 [2,] 2  7 12

 [3,] 3  8 13

 [4,] 4  9 14

 [5,] 5 10 15

Create a matrix where x, y and z are rows

rbind(x, y, z)

   [,1] [,2] [,3] [,4] [,5]

 x    1    2    3    4    5

 y    6    7    8    9   10

 z   11   12   13   14   15

Finaly, you can create a matrix using dim() method:

              elements <- c(1,2,3,4,5,6,7,8,9)

dim(elements)<- c(3,3)

elements

     [,1] [,2] [,3]

[1,]    1    4    7

[2,]    2    5    8

[3,]    3    6    9

We have seen basic methods and ways of creating the matrix. You can also combine those functions with others.

For example, you can use seq() function in creating a matrix:

x <- matrix(seq(1,10,2),nrow=3)

              x

     [,1] [,2]

[1,]    1    7

[2,]    3    9

[3,]    5    1

We specified numbers from 1 to 10, with 2 as a stepping stone. You notice how we didn’t get 11 after 9, but 1? That is because matrix() function needs to fill all fields in making a matrix, it can’t leave empty space as one might expect.

You can put conditions on which elements will be in your matrix. For example, lets see how to put only even numbers from 1-9 list:

              our_row <- c(1,2,3,4,5,6,7,8,9)

 our_matrix <- matrix(our_row[our_row%%2==0], nrow=2)

              our_matrix

                  [,1] [,2]

[1,]    2    6

[2,]    4    8

Create a matrix only with numbers which are higher than 5:

our_row <- c(1,2,3,4,5,6,7,8,9)

our_matrix <- matrix(our_row[our_row>5], nrow=1)

our_matrix

     [,1] [,2] [,3]

[1,]    6    7    8

In the same way, you can create matrix which contain characters:

our_row <- c(“first”,”second”,”third”,”fourth”,”fifth”,”sixth”)

 our_matrix <- matrix(our_row, nrow=1)

 our_matrix

     [,1]    [,2]     [,3]    [,4]     [,5]    [,6]  

[1,] “first” “second” “third” “fourth” “fifth” “sixth”

 You can use conditionals, just like we did on numeric matrix:   

our_countries <- c(“China”,”France”,”India”,”Venezuela”,”Argentina”)

asia <- c(“China”,”India”)

our_matrix <- matrix(our_countries[our_countries %in% asia], nrow=1)

our_matrix

     [,1]    [,2]  

[1,] “China” “India”

These were the most common ways of creating a matrix. If you want to learn more about different operations you can do on matrix, please check out our tutorial, Matrices and operations with them.

Scroll to top
Privacy Policy