Explain general syntax to create Matrices in R?

Question

Please explain the parameters as well

in progress 0
Shashi Jain 55 years 2 Answers 936 views Newbie 0

Answers ( 2 )

  1. The syntax goes like

    matrix(data, nrow, ncol, byrow, dimnames)

    where
    data is the input vector which becomes the data elements of the matrix.
    nrow is the number of rows to be created.
    ncol is the number of columns to be created.
    byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
    dimname is the names assigned to the rows and columns.

  2. Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types.

    A Matrix is created using the matrix() function.

    Syntax:
    matrix(data, nrow, ncol, byrow, dimnames)
    Parameters:
    data is the input vector which becomes the data elements of the matrix.
    nrow is the number of rows to be created.
    ncol is the number of columns to be created.
    byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
    dimname is the names assigned to the rows and columns.

    # Define the column and row names.
    rownames = c(“row1”, “row2”, “row3”, “row4”)
    colnames = c(“col1”, “col2”, “col3”)

    P <- matrix(c(3:14), nrow = 4, ncol=3, byrow = TRUE, dimnames = list(rownames, colnames))
    print(P)

    col1 col2 col3
    row1 3 4 5
    row2 6 7 8
    row3 9 10 11
    row4 12 13 14

Leave an answer

Browse
Browse