- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between creating a matrix by using matrix function or as.matrix function in R?
The difference between as.matrix and matrix function is that nrow argument or ncol argument are not helpful with as.matrix function but with matrix function we can use them. Therefore, we can actual define a matrix with matrix function but if we have a data frame or data table then it can be converted to matrix by using as.matrix function.
Examples of creating matrix with as.matrix and matrix function
Example1
M<−as.matrix(1:25,nrow=5) M
Output
[,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12 [13,] 13 [14,] 14 [15,] 15 [16,] 16 [17,] 17 [18,] 18 [19,] 19 [20,] 20 [21,] 21 [22,] 22 [23,] 23 [24,] 24 [25,] 25
Example2
M<−as.matrix(1:25,ncol=5) M
Output
[,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12 [13,] 13 [14,] 14 [15,] 15 [16,] 16 [17,] 17 [18,] 18 [19,] 19 [20,] 20 [21,] 21 [22,] 22 [23,] 23 [24,] 24 [25,] 25
Example3
M<−matrix(1:25,ncol=5) M
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25
Example4
M<−matrix(1:25,nrow=5) M
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25
Example5
M<−matrix(1:25,nrow=5,byrow=TRUE) M
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 6 7 8 9 10 [3,] 11 12 13 14 15 [4,] 16 17 18 19 20 [5,] 21 22 23 24 25
Example6
M<−matrix(1:25,nrow=5,byrow=FALSE) M
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25
- Related Articles
- How to create a matrix using vector generated with rep function in R?
- The CSS matrix() Function
- How to extract diagonal elements of a matrix in R without using diag function?
- What is the difference between order and rank function in R?
- What is the difference between class and typeof function in R?
- Search a string in Matrix Using Split function in Java
- How to find the column and row indices of values in a matrix using which function in R?
- How to create a block diagonal matrix using a matrix in R?
- How to check in R whether a matrix element is present in another matrix or not?
- What is the difference between a method and a function?
- What is the difference between: var functionName = function() {} and function functionName() {} in Javascript
- C++ Program to Multiply two Matrices by Passing Matrix to Function
- How to check if a matrix is invertible or not in R?
- How to replicate a matrix by rows in R?
- How to remove a column from matrix in R by using its name?

Advertisements