---
description: In this tutorial, you will learn How to Create a Matrix Function in R. Learn how to add a column to a matrix, Slice a Matrix with detailed step-by-step examples.
title: Matrix Function in R: Create, Print, add Column &#038; Slice
image: https://www.guru99.com/images/r-matrix.png
---

 

[Skip to content](#main) 

## Matrix Function in R

A matrix function in R is a 2-dimensional array that has m number of rows and n number of columns. In other words, matrix in R programming is a combination of two or more vectors with the same data type.

**Note:** It is possible to create more than two dimensions arrays with matrix function in R.

[![Matrix Function in R](https://www.guru99.com/images/r_programming/032918_1422_RMatrixTuto1.png)](https://www.guru99.com/images/r%5Fprogramming/032918%5F1422%5FRMatrixTuto1.png)

## How to Create a Matrix in R

We can create a matrix with the function matrix(). Following is a function to create a matrix in R which takes three arguments:

matrix(data, nrow, ncol, byrow = FALSE)

**Arguments:** 

* **data**: The collection of elements that R will arrange into the rows and columns of the matrix \\
* **nrow**: Number of rows
* **ncol**: Number of columns
* **byrow**: The rows are filled from the left to the right. We use \`byrow = FALSE\` (default values), if we want the matrix to be filled by the columns i.e. the values are filled top to bottom.

Let’s construct two 5×2 matrix with a sequence of number from 1 to 10, one with byrow = TRUE and one with byrow = FALSE to see the difference.

# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow =  TRUE 
matrix_a <-matrix(1:10, byrow = TRUE, nrow = 5)
matrix_a

**Output:**

[![Create a Matrix in R](https://www.guru99.com/images/r_programming/032918_1422_RMatrixTuto2.png)](https://www.guru99.com/images/r%5Fprogramming/032918%5F1422%5FRMatrixTuto2.png)

### Print dimension of the matrix with dim()

Now, let’s print dimension of the matrix in R with dim(). The syntax to print matrix in R using dim() is:

# Print dimension of the matrix with dim()
dim(matrix_a)

**Output:**

## [1] 5 2

### Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow = FALSE

# Construct a matrix with 5 rows that contain the numbers 1 up to 10 and byrow =  FALSE
matrix_b <-matrix(1:10, byrow = FALSE, nrow = 5)
matrix_b

**Output:**

[![Matrix with 5 Rows that Contain byrow = FALSE](https://www.guru99.com/images/r_programming/032918_1422_RMatrixTuto3.png)](https://www.guru99.com/images/r%5Fprogramming/032918%5F1422%5FRMatrixTuto3.png)

### Print dimension of the matrix with dim()

Again, print the dimension of the matrix using dim(). Below is a syntax of R print matrix dimension:

# Print dimension of the matrix with dim()
dim(matrix_b)

**Output:**

## [1] 5 2

**Note**: Using command matrix\_b <-matrix(1:10, byrow = FALSE, ncol = 2) will have same effect as above.

You can also create a 4×3 matrix using ncol. R will create 3 columns and fill the row from top to bottom. Check an example

matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3)
matrix_c

**Output:**

##       [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12

**Example:**

dim(matrix_c)

**Output:**

## [1] 4 3

### RELATED ARTICLES

* [R Tutorial for Beginners ](https://www.guru99.com/r-tutorial.html "R Tutorial for Beginners")
* [R Data Frame: How to Create, Append, Select & Subset ](https://www.guru99.com/r-data-frames.html "R Data Frame: How to Create, Append, Select & Subset")
* [How to Replace Missing Values(NA) in R: na.omit & na.rm ](https://www.guru99.com/r-replace-missing-values.html "How to Replace Missing Values(NA) in R: na.omit & na.rm")
* [Decision Tree in R: Classification Tree with Example ](https://www.guru99.com/r-decision-trees.html "Decision Tree in R: Classification Tree with Example")

## Add a Column to a Matrix with the cbind()

You can add column to matrix R with the cbind() command. cbind() means column binding.cbind() can concatenate as many matrix or columns as specified. For example, our previous example created a 5×2 matrix. We concatenate a third column and verify the dimension is 5×3

**Example:**

# concatenate c(1:5) to the matrix_a
matrix_a1 <- cbind(matrix_a, c(1:5))
# Check the dimension
dim(matrix_a1)

**Output:**

## [1] 5 3

**Example:**

matrix_a1

**Output**

##       [,1] [,2] [,3]
## [1,]    1    2    1
## [2,]    3    4    2
## [3,]    5    6    3
## [4,]    7    8    4
## [5,]    9   10    5

**Example:**

We can also add column to matrix R, more than one time. Let’s see the next sequence of number to the matrix\_a2 matrix. The dimension of new matrices in R will be 4×6 with number from 1 to 24.

matrix_a2 <-matrix(13:24, byrow = FALSE, ncol = 3)

**Output:**

##      [,1] [,2] [,3]
## [1,]   13   17   21
## [2,]   14   18   22
## [3,]   15   19   23
## [4,]   16   20   24

**Example:**

matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3)		
matrix_d <- cbind(matrix_a2, matrix_c)
dim(matrix_d)

**Output:**

## [1] 4 6

**NOTE**: The number of rows of matrices in R should be equal for cbind work

cbind()concatenate columns, rbind() appends rows. Let’s add one row to our matrix\_c matrix and verify the dimension is 5×3

matrix_c <-matrix(1:12, byrow = FALSE, ncol = 3)
# Create a vector of 3 columns
add_row <- c(1:3)
# Append to the matrix
matrix_c <- rbind(matrix_c, add_row)
# Check the dimension
dim(matrix_c)

**Output:**

## [1] 5 3

## Slice a Matrix

We can select elements one or many elements from a matrix in [R programming](https://www.guru99.com/r-programming-introduction-basics.html) by using the square brackets \[ \]. This is where slicing comes into the picture.

**For example:**

* matrix\_c\[1,2\] selects the element at the first row and second column.
* matrix\_c\[1:3,2:3\] results in a R slice matrix with the data on the rows 1, 2, 3 and columns 2, 3,
* matrix\_c\[,1\] selects all elements of the first column.
* matrix\_c\[1,\] selects all elements of the first row.

**Here is the output you get for the above codes**

[![Slice a Matrix](https://www.guru99.com/images/r_programming/032918_1422_RMatrixTuto4.png)](https://www.guru99.com/images/r%5Fprogramming/032918%5F1422%5FRMatrixTuto4.png)

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter](https://www.guru99.com/images/footer-email-avatar-imges-1.png) Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/r-matrix.png","url":"https://www.guru99.com/images/r-matrix.png","width":"242","height":"200","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/r-matrix-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/r-programming","name":"R Programming"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/r-matrix-tutorial.html","name":"Matrix Function in R: Create, Print, add Column &#038; Slice"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/r-matrix-tutorial.html#webpage","url":"https://www.guru99.com/r-matrix-tutorial.html","name":"Matrix Function in R: Create, Print, add Column &#038; Slice","dateModified":"2024-06-12T19:17:28+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/r-matrix.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/r-matrix-tutorial.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/evelyn","name":"Evelyn Clarke","description":"I'm Evelyn Clarke, an AI Research Scientist specializing in machine learning and natural language processing, dedicated to advancing the field responsibly.","url":"https://www.guru99.com/author/evelyn","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/evelyn-clarke-author-120x120.png","url":"https://www.guru99.com/images/evelyn-clarke-author-120x120.png","caption":"Evelyn Clarke","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"@type":"NewsArticle","headline":"Matrix Function in R: Create, Print, add Column &#038; Slice","keywords":"R, Programming","dateModified":"2024-06-12T19:17:28+05:30","articleSection":"R Programming","author":{"@id":"https://www.guru99.com/author/evelyn","name":"Evelyn Clarke"},"publisher":{"@id":"https://www.guru99.com/#organization"},"description":"In this tutorial, you will learn How to Create a Matrix Function in R. Learn how to add a column to a matrix, Slice a Matrix with detailed step-by-step examples.","copyrightYear":"2024","copyrightHolder":{"@id":"https://www.guru99.com/#organization"},"name":"Matrix Function in R: Create, Print, add Column &#038; Slice","@id":"https://www.guru99.com/r-matrix-tutorial.html#richSnippet","isPartOf":{"@id":"https://www.guru99.com/r-matrix-tutorial.html#webpage"},"image":{"@id":"https://www.guru99.com/images/r-matrix.png"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/r-matrix-tutorial.html#webpage"}}]}
```
