w3resource

Access Specific Elements, Rows, and Columns in an R Matrix

R Programming: Matrix Exercise-4 with Solution

Write a R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix.

Sample Solution:

R Programming Code:

# Define row names for the matrix
row_names = c("row1", "row2", "row3", "row4")

# Define column names for the matrix
col_names = c("col1", "col2", "col3", "col4")

# Create a matrix with values from 1 to 16, specifying number of rows, filling by row, and naming rows and columns
M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))

# Print a message indicating that the following output is the original matrix
print("Original Matrix:")

# Print the matrix to the console
print(M)

# Print a message indicating that the following output is the element at 3rd column and 2nd row
print("Access the element at 3rd column and 2nd row:")

# Access and print the element located at the 2nd row and 3rd column of the matrix
print(M[2,3])

# Print a message indicating that the following output is the 3rd row of the matrix
print("Access only the 3rd row:")

# Access and print only the 3rd row of the matrix
print(M[3,])

# Print a message indicating that the following output is the 4th column of the matrix
print("Access only the 4th column:")

# Access and print only the 4th column of the matrix
print(M[,4])

Output:

[1] "Original Matrix:"
     col1 col2 col3 col4
row1    1    2    3    4
row2    5    6    7    8
row3    9   10   11   12
row4   13   14   15   16
[1] "Access the element at 3rd column and 2nd row:"
[1] 7
[1] "Access only the  3rd row:"
col1 col2 col3 col4 
   9   10   11   12 
[1] "Access only the 4th column:"
row1 row2 row3 row4 
   4    8   12   16                 

Explanation:

  • Define Row Names:
    • row_names = c("row1", "row2", "row3", "row4")
      • Creates a vector with names for the rows of the matrix.
  • Define Column Names:
    • col_names = c("col1", "col2", "col3", "col4")
      • Creates a vector with names for the columns of the matrix.
  • Create Matrix:
    • M = matrix(c(1:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
      • Creates a 4x4 matrix filled with numbers from 1 to 16.
      • nrow = 4: Specifies that the matrix should have 4 rows.
      • byrow = TRUE: Fills the matrix by rows (row-major order).
      • dimnames = list(row_names, col_names): Assigns row and column names to the matrix.
  • Print Original Matrix:
    • print("Original Matrix:")
      • Prints a message indicating that the next output is the original matrix.
    • print(M)
      • Prints the contents of the matrix M.
  • Access Element at Specific Position:
    • print("Access the element at 3rd column and 2nd row:")
      • Prints a message indicating that the next output is the element at the 2nd row and 3rd column.
    • print(M[2,3])
      • Accesses and prints the element located at the 2nd row and 3rd column of the matrix.
  • Access Specific Row:
    • print("Access only the 3rd row:")
      • Prints a message indicating that the next output is the 3rd row of the matrix.
    • print(M[3,])
      • Accesses and prints all elements of the 3rd row of the matrix.
  • Access Specific Column:
    • print("Access only the 4th column:")
      • Prints a message indicating that the next output is the 4th column of the matrix.
    • print(M[,4])
      • Accesses and prints all elements of the 4th column of the matrix.

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 a matrix taking a given vector of numbers as input and define the column and row names. Display the matrix.
Next: Write a R program to create two 2x3 matrix and add, subtract, multiply and divide the matrixes.

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://www.w3resource.com/r-programming-exercises/matrix/r-programming-matrix-exercise-4.php