1. Creating a Matrix Using the matix( ) Function
my_matrix1 = matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
print(my_matrix1)
Explanation:
- The function
matrix()is used to create a two-dimensional data structure in R. - The first argument
c(1,2,3,4,5,6)represents a vector of elements that will be placed inside the matrix. nrow = 3specifies that the matrix should have 3 rows.ncol = 2specifies that the matrix should have 2 columns.
By default, R fills the matrix column-wise. This means:
- The values
1, 2, 3fill the first column from top to bottom. - The values
4, 5, 6fill the second column.
The print() function simply displays the matrix on the console so that the user can visually inspect the structure.
2. Creating a Matrix with Row-wise Filling
my_matrix2 = matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3, byrow = TRUE)
print(my_matrix2)
Explanation:
- This code again uses the
matrix()function with the same elements. - Here,
nrow = 2andncol = 3define a matrix with 2 rows and 3 columns. - The argument
byrow = TRUEinstructs R to fill the matrix row-wise instead of column-wise.
As a result:
- The first row contains the values
1, 2, 3. - The second row contains the values
4, 5, 6.
This option is particularly useful when data is naturally organized row by row, such as student records or observations in experiments.
3. Assigning Row Names and Column Names
rownames(my_matrix2) = c('row1', 'row2')
colnames(my_matrix2) = c('col1', 'col2', 'col3')
Explanation:
rownames()is used to assign meaningful names to each row of the matrix.colnames()is used to assign descriptive names to each column.
Instead of identifying elements using numeric indices alone, naming rows and columns makes the matrix easier to interpret and use in analysis.
For example, rows might represent individuals, and columns might represent different variables such as marks or measurements.
4. Understanding the Internal Structure of a Matrix
str(my_matrix2)
Explanation:
- The
str()function displays the internal structure of an R object. - It shows:
- The data type of the matrix (numeric in this case)
- The dimensions (number of rows and columns)
- The order in which elements are stored
- Any associated attributes such as row names and column names
This function is extremely helpful for debugging and verifying whether the data has been created as expected.
5. Accessing an Element of a Matrix
my_matrix2[1, 2]
Explanation:
- Matrix elements are accessed using indexing.
- The first number inside the square brackets refers to the row index.
- The second number refers to the column index.
In this case:
1refers to the first row2refers to the second column
So, this statement retrieves the element located at row 1 and column 2 of the matrix.
Note: R follows 1-based indexing, meaning counting starts from 1, not 0.
6. Key Points for Students
- A matrix stores homogeneous data only (all elements must be of the same type).
- The
matrix()function requires elements, number of rows, and number of columns. - By default, values are filled column-wise.
- Use
byrow = TRUEto fill values row-wise. - Naming rows and columns improves readability and usability.
- The
str()function helps understand how R internally stores the matrix. - Indexing uses the format
[row, column].
Practice Questions – Matrix in R
The following practice questions are designed based strictly on the Matrix in R. The questions progress from basic understanding to advanced conceptual and application-level problems, suitable for MSc Data Science students. These questions aim to strengthen both conceptual clarity and practical coding skills.
Section A: Basic Level (Foundational Understanding)
- What is a matrix in R? How is it different from a vector?
- Which function is used to create a matrix in R? List its mandatory arguments.
- Explain why a matrix in R can store only homogeneous data.
- What does the argument
nrowspecify when creating a matrix? - By default, how does R arrange elements inside a matrix? Explain with an example.
Section B: Intermediate Level (Conceptual and Code-Based)
- What is the purpose of the
byrowargument in thematrix()function? - Consider the following code:
matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
Explain how the elements are placed inside the matrix.
- Write an R command to create a matrix with 2 rows and 4 columns using the numbers 1 to 8, filled row-wise.
- Why is it useful to assign row names and column names to a matrix? Explain with practical relevance.
- Which functions are used to assign names to rows and columns in a matrix?
Section C: Advanced Level (Analytical and Application-Oriented)
- Explain the internal structure of a matrix as revealed by the
str()function. - What information does the
str()function provide that is not immediately visible from printing the matrix? - How does R internally store matrix elements, even when the matrix appears two-dimensional?
- Explain the indexing method used to access elements of a matrix in R.
- What does the following statement return and why?
my_matrix2[1, 2]
Section D: Critical Thinking and Error Analysis
- What happens if the number of elements supplied to the
matrix()function does not match the specified number of rows and columns? Explain R’s behavior. - Predict the output and explain the reasoning:
matrix(1:6, nrow = 2, ncol = 3)
- Identify and explain the mistake, if any, in the following code:
matrix(c(1,2,3,4), nrow = 3, ncol = 2)
- Why is understanding column-wise filling important when working with real-world datasets?
- Discuss a real-life data analysis scenario where using a matrix would be more appropriate than using a data frame.