Here are three ways to create an empty matrix in R:
- Using row and column
- Using only row
- Using only column
Method 1: Using a row and column
mat <- matrix(, nrow = 3, ncol = 2)
print(mat)
Output
[,1] [,2]
[1,] NA NA
[2,] NA NA
[3,] NA NA
We created a 3×2 matrix mat with three rows and two columns, initially filled with NA values because there is no data and it is empty.
Method 2: Using only a row
mat <- matrix(, nrow = 5, ncol = 0)
print(mat)
Output
[1,]
[2,]
[3,]
[4,]
[5,]
We created a matrix mat with 5 rows and 0 columns.
Method 3: Use the column
mtrx <- matrix(ncol = 4, nrow = 0)
mtrx
Output
[,1] [,2] [,3] [,4]
That is it.
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.