WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

R · Basic · question 10 of 100

What are the basic operations you can perform on a matrix in R?

📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

In R, a matrix is a two-dimensional array in which each element has the same data type. Matrices are a useful data structure for organizing and manipulating data, and R provides several operations that can be performed on matrices.

Creating a matrix: You can create a matrix in R using the matrix() function. The matrix() function takes the data to be arranged in the matrix, the number of rows and columns, and other optional arguments such as row and column names. Here’s an example:

    # Creating a 3x3 matrix
    m <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3)
    
    # Displaying the matrix
    m

In this example, the matrix() function creates a 3x3 matrix with the values 1 through 9.

Accessing elements of a matrix: You can access individual elements of a matrix using the square bracket notation. The first index specifies the row, and the second index specifies the column. Here’s an example:

    # Accessing the element in the second row and third column
    m[2, 3]

In this example, the square brackets [] are used to access the element in the second row and third column of the matrix m.

Performing operations on matrices: R provides several functions for performing mathematical operations on matrices. Some of the basic operations you can perform on a matrix include: Addition: You can add two matrices of the same size element-wise using the + operator.

    # Adding two matrices
    m1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
    m2 <- matrix(c(2, 4, 6, 8, 10, 12), nrow = 2, ncol = 3)
    m_sum <- m1 + m2
    
    # Displaying the sum matrix
    m_sum

In this example, we create two 2x3 matrices m1 and m2, and add them element-wise using the + operator to create a new 2x3 matrix m_sum.

Subtraction: You can subtract two matrices of the same size element-wise using the - operator.

    # Subtracting two matrices
    m_diff <- m2 - m1
    
    # Displaying the difference matrix
    m_diff

In this example, we subtract m1 from m2 element-wise using the - operator to create a new 2x3 matrix m_diff.

Multiplication: You can multiply two matrices using the %% operator.

    # Multiplying two matrices
    m1 <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
    m2 <- matrix(c(2, 1, 3, 2), nrow = 2, ncol = 2)
    m_prod <- m1 %*% m2
    
    # Displaying the product matrix
    m_prod

In this example, we multiply m1 and m2 using the %% operator to create a new 2x2 matrix m_prod.

Transposing a matrix: You can transpose a matrix using the t() function, which swaps the rows and columns of the matrix.

    # Transposing a matrix
    m_transpose <- t(m)
    
    # Displaying the transposed matrix
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic R interview — then scores it.
📞 Practice R — free 15 min
📕 Buy this interview preparation book: 100 R questions & answers — PDF + EPUB for $5

All 100 R questions · All topics