0
0
R Programmingprogramming~10 mins

Matrix creation in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Matrix creation
Start with vector of elements
Call matrix() function
Specify rows and columns
Fill matrix by columns (default) or rows
Matrix created and stored
Create a matrix by providing a vector of elements and specifying the number of rows and columns. The matrix fills by columns by default.
Execution Sample
R Programming
vec <- 1:6
mat <- matrix(vec, nrow=2, ncol=3)
print(mat)
Creates a 2-row, 3-column matrix from numbers 1 to 6 and prints it.
Execution Table
StepActionVector ElementsMatrix ShapeMatrix Content
1Create vector vec1,2,3,4,5,6--
2Call matrix(vec, nrow=2, ncol=3)1,2,3,4,5,62 rows, 3 cols-
3Fill matrix by columns1,2,3,4,5,62x3[1 3 5 2 4 6]
4Print matrix-2x3 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
💡 Matrix created with 2 rows and 3 columns filled column-wise from vector elements.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
vec-c(1,2,3,4,5,6)c(1,2,3,4,5,6)c(1,2,3,4,5,6)c(1,2,3,4,5,6)
mat--matrix(2,3) unfilledmatrix(2,3) filled column-wisematrix(2,3) filled column-wise
Key Moments - 2 Insights
Why does the matrix fill by columns and not by rows?
By default, R's matrix() fills elements column-wise as shown in step 3 of the execution_table. To fill by rows, you must set byrow=TRUE.
What happens if the number of elements in the vector does not match rows*columns?
R recycles elements to fill the matrix. This is not shown here but is important to know for matrix creation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the element at row 2, column 3 of the matrix?
A6
B5
C3
D4
💡 Hint
Check the matrix content column-wise filling in step 3 of execution_table.
At which step is the matrix shape defined?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Matrix Shape' column in execution_table.
If we add byrow=TRUE in matrix(), how would the matrix content change at step 3?
AMatrix fills diagonally
BMatrix fills row-wise: [1 2 3; 4 5 6]
CMatrix fills column-wise: [1 3 5; 2 4 6]
DMatrix remains empty
💡 Hint
Recall the default filling is column-wise; byrow=TRUE changes it to row-wise.
Concept Snapshot
matrix(data, nrow, ncol, byrow=FALSE)
- Creates a matrix from data vector
- nrow and ncol set dimensions
- byrow=FALSE fills columns first
- byrow=TRUE fills rows first
- Recycling if data length < nrow*ncol
Full Transcript
This visual execution shows how to create a matrix in R. First, a vector of elements is created. Then the matrix() function is called with the vector and specified rows and columns. By default, the matrix fills column-wise with the vector elements. The final matrix is printed showing the elements arranged in 2 rows and 3 columns. Key points include the default column-wise filling and the ability to change it with byrow=TRUE. The variable tracker shows how the vector and matrix variables change during execution.