Create Empty Matrix in R (Example)

 

In this R programming article you’ll learn how to create an empty matrix with zero rows.

The content of the article looks as follows:

You’re here for the answer, so let’s get straight to the R syntax…

 

Example: Creating Matrix with Zero Rows

The following syntax explains how to create an empty matrix in the R programming language by using the matrix() function and the ncol and nrow arguments.

my_mat <- matrix(ncol = 3, nrow = 0)             # Applying matrix() function
my_mat                                           # Printing matrix to console
# [,1] [,2] [,3]

Have a look at the previous RStudio console output: It shows our matrix with zero rows.

We can also modify the column names of our matrix using the colnames function:

colnames(my_mat) <- c("Col1", "Col2", "Col3")    # Rename columns of matrix
my_mat                                           # Printing matrix to console
# Col1 Col2 Col3

Looks good!

 

Video, Further Resources & Summary

In case you need further explanations on the R codes of this tutorial, I can recommend watching the following video instruction that I have published on my YouTube channel. In the video, I show the R programming code of this tutorial:

 

 

Furthermore, you could read the other R articles on this website. I have released numerous articles about matrices and data manipulation already.

 

In this R tutorial you learned how to construct empty matrices. Let me know in the comments below, in case you have further questions and/or comments. Furthermore, don’t forget to subscribe to my email newsletter to get updates on the newest tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top