w3resource

R Programming: Create and Fill 5x4, 3x3 Matrices with Labels

R Programming: Basic Exercise-16 with Solution

Write a R program to create a 5 × 4 matrix , 3 × 3 matrix with labels and fill the matrix by rows and 2 × 2 matrix with labels and fill the matrix by columns.

Sample Solution :

R Programming Code :

# Create a 5x4 matrix with elements from 1 to 20
m1 = matrix(1:20, nrow=5, ncol=4)

# Print a message indicating the following matrix is 5x4
print("5 × 4 matrix:")

# Print the 5x4 matrix
print(m1)

# Define a vector of cells for a 3x3 matrix
cells = c(1,3,5,7,8,9,11,12,14)

# Define row names for the 3x3 matrix
rnames = c("Row1", "Row2", "Row3")

# Define column names for the 3x3 matrix
cnames = c("Col1", "Col2", "Col3")

# Create a 3x3 matrix with specified cells, filled by rows, and with row and column names
m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE, dimnames=list(rnames, cnames))

# Print a message indicating the following matrix is 3x3 with labels, filled by rows
print("3 × 3 matrix with labels, filled by rows: ")

# Print the 3x3 matrix filled by rows with labels
print(m2)

# Print a message indicating the following matrix is 3x3 with labels, filled by columns
print("3 × 3 matrix with labels, filled by columns: ")

# Create a 3x3 matrix with specified cells, filled by columns, and with row and column names
m3 = matrix(cells, nrow=3, ncol=3, byrow=FALSE, dimnames=list(rnames, cnames))

# Print the 3x3 matrix filled by columns with labels
print(m3)

Output:

[1] "5 × 4 matrix:"
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
[1] "3 × 3 matrix with labels, filled by rows: "
     Col1 Col2 Col3
Row1    1    3    5
Row2    7    8    9
Row3   11   12   14
[1] "3 × 3 matrix with labels, filled by columns: "
     Col1 Col2 Col3
Row1    1    7   11
Row2    3    8   12
Row3    5    9   14                         

Explanation:

  • Create a 5 × 4 Matrix:
    • m1 = matrix(1:20, nrow=5, ncol=4): Creates a 5x4 matrix with numbers from 1 to 20, filled column-wise by default.
    • print("5 × 4 matrix:"): Prints a label for the matrix.
    • print(m1): Displays the 5x4 matrix.
  • Create a 3 × 3 Matrix with Labels (Filled by Rows):
    • cells = c(1,3,5,7,8,9,11,12,14): Defines the values to be used in the matrix.
    • rnames = c("Row1", "Row2", "Row3"): Specifies row names for the matrix.
    • cnames = c("Col1", "Col2", "Col3"): Specifies column names for the matrix.
    • m2 = matrix(cells, nrow=3, ncol=3, byrow=TRUE, dimnames=list(rnames, cnames)): Creates a 3x3 matrix using the cells vector, filled by rows (byrow=TRUE), and assigns row and column names.
    • print("3 × 3 matrix with labels, filled by rows: "): Prints a label for this matrix.
    • print(m2): Displays the 3x3 matrix filled by rows with labels.
  • Create a 3 × 3 Matrix with Labels (Filled by Columns):
    • m3 = matrix(cells, nrow=3, ncol=3, byrow=FALSE, dimnames=list(rnames, cnames)): Creates a 3x3 matrix using the cells vector, filled by columns (byrow=FALSE), and assigns row and column names.
    • print("3 × 3 matrix with labels, filled by columns: "): Prints a label for this matrix.
    • print(m3): Displays the 3x3 matrix filled by columns with labels.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create three vectors numeric data, character data and logical data. Display the content of the vectors and their type.
Next: Write a R program to create an array, passing in a vector of values and a vector of dimensions, also provide names for each dimension.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-16.php