Explain general syntax to create Matrices in R?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 2 )
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.
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