The “matrix” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Creates a matrix, a two-dimensional array with rows and columns.

  • General Class: Data Manipulation

  • Required Argument(s):

    • The data to be used in the matrix.

  • Notable Optional Arguments:

    • byrow: A logical value. If TRUE, fill matrix by rows, otherwise by columns.

    • dimnames: A list with the names for the rows and columns.

    • ncol: Number of columns in the matrix.

    • nrow: Number of rows in the matrix.

  • Example:

  • # Example data for using the matrix function
    values <- 1:6

    # Create a matrix with two rows and three columns
    mat <- matrix(values, nrow = 2, ncol = 3, byrow = TRUE)

    # Display the result
    print(mat)

  • In this example, the matrix function is used to create a 2x3 matrix (mat) with data from the values vector. The nrow and ncol arguments specify the dimensions of the matrix, and byrow is set to TRUE to fill the matrix by rows. The resulting object is a matrix that can be used for various mathematical and data manipulation operations.

Previous
Previous

The “list” Function in R

Next
Next

The “data.frame” Function in R