What is Matrix in R? Explained with Examples

In this R Matrix Tutorial with examples, you will learn what is Matrix (matrices)? its features, its advantages, and how to use Matrix in real-time with sample examples. Also, you will learn how to create Matrix, modify its’ values, accessing its values.

All examples provided in this R Matrix are basic, simple, and easy to practice for beginners who are enthusiastic to learn R and advance their careers.

Note: In case you canâ€™t find the Matrix examples you are looking for on this tutorial page, I would recommend using the Search option from the menu bar to find your tutorial and sample example code.

1. What is R Matrix?

Matix is also called Matrices which is an R data structure that is used to store data in two-dimensional rows and columns. Below are some key points to know and remember about Matrix.

• Matrices or Matrix is a data structure.
• The matrix is two-dimensional.
• It stores data in rows and columns.
• All elements in Matrix should be of the same type (numeric, logical, character, etc.).
• Matrix is a collection of two or more R Vectors.
• R Data Frame is another data structure with two dimensional to store elements of different data types.

2. Create Matrix

By using the matrix() function you can create a matrix object in R Programming Language.

``````
# Create R matrix
data <- c(10,11,12,13,14,15,16,17,18)
mtx <- matrix(data,nrow=3,ncol=3,byrow=TRUE)
mtx
``````

Yields below output.

``````
[,1] [,2] [,3]
[1,]   10   11   12
[2,]   13   14   15
[3,]   16   17   18
``````

Conclusion

In this article, you have learned what is a matrix in R Programming? how to create it? and access its elements. Matrix is a data structure that is used to store values in two-dimensional rows and columns and all elements should be of the same basic type.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..