R Matrices

R Matrices

🧮 R Matrices

Matrices in R are two-dimensional data structures that store data in rows and columns.

All elements in a matrix must be of the same data type.


 1. Creating a Matrix

Use the matrix() function.


📌 By default, R fills matrices column-wise.


 2. Fill Matrix by Row



 3. Matrix Indexing



 4. Matrix Dimensions



 5. Named Rows and Columns



 6. Matrix Arithmetic (Element-wise)


 


 7. Matrix Multiplication

Use %*% operator.


📌 %*%*


 8. Transpose of Matrix



 9. Combine Rows and Columns

Add rows


Add columns



10. Check if Object is Matrix



 11. Convert Vector to Matrix



 Matrices vs Data Frames

Feature Matrix Data Frame
Dimensions 2D 2D
Data type Same Different
Usage Math operations Data analysis

📌 Summary

  • Matrices are 2D structures

  • All elements must be same type

  • Support mathematical operations

  • Column-wise filling by default

You may also like...