What is the difference between creating a matrix by using matrix function or as.matrix function in R?

R ProgrammingServer Side ProgrammingProgramming

<p>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.</p><p>Examples of creating matrix with as.matrix and matrix function</p><h2>Example1</h2><p><a class="demo" href="http://tpcg.io/5mEffjr8" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">M&lt;&minus;as.matrix(1:25,nrow=5) M</pre><h2>Output</h2><pre class="result notranslate"> [,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</pre><h2>Example2</h2><p><a class="demo" href="http://tpcg.io/AZ677kqQ" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="result notranslate">M&lt;&minus;as.matrix(1:25,ncol=5) M</pre><h2>Output</h2><pre class="result notranslate"> [,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</pre><h2>Example3</h2><p><a class="demo" href="http://tpcg.io/QDseuCqG" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="result notranslate">M&lt;&minus;matrix(1:25,ncol=5) M</pre><h2>Output</h2><pre class="result notranslate" style="">&nbsp; &nbsp;[,1] [,2] [,3] [,4] [,5] [1,] 1 &nbsp; 6 &nbsp; &nbsp;11 &nbsp; 16 &nbsp; 21 [2,] 2 &nbsp; 7 &nbsp; &nbsp;12 &nbsp; 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25</pre><h2>Example4</h2><p><a class="demo" href="http://tpcg.io/ZFrulkUJ" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">M&lt;&minus;matrix(1:25,nrow=5) M</pre><h2>Output</h2><pre class="result notranslate">[,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</pre><h2>Example5</h2><p><a class="demo" href="http://tpcg.io/7SNS9yqP" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="result notranslate">M&lt;&minus;matrix(1:25,nrow=5,byrow=TRUE) M</pre><h2>Output</h2><pre class="result notranslate" style=""> [,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</pre><h2>Example6</h2><p><a class="demo" href="http://tpcg.io/36pU7Fay" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="result notranslate">M&lt;&minus;matrix(1:25,nrow=5,byrow=FALSE) M</pre><h2>Output</h2><pre class="result notranslate" style="">&nbsp; &nbsp;[,1] [,2] [,3] [,4] [,5] [1,] 1 &nbsp; 6 &nbsp; 11 &nbsp; 16 21 [2,] 2 &nbsp; 7 &nbsp; 12 &nbsp; 17 22 [3,] 3 &nbsp; 8 &nbsp; 13 &nbsp; 18 23 [4,] 4 &nbsp; 9 &nbsp; 14 &nbsp; 19 24 [5,] 5 &nbsp; 10 15 &nbsp; 20 25</pre>
raja
Updated on 06-Nov-2020 13:53:43

Advertisements