How to divide the matrix rows by row maximum in R?

R ProgrammingServer Side ProgrammingProgramming

<p>To divide matrix row values by row maximum in R, we can follow the below steps &minus;</p><ul class="list"><li>First of all, create a matrix.</li><li>Then, use apply function to divide the matrix row values by row maximum.</li></ul><h2>Create the matrix</h2><p>Let&rsquo;s create a matrix as shown below &minus;</p><p><a class="demo" href="http://tpcg.io/r2o7I4Gr " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">M&lt;-matrix(sample(1:10,75,replace=TRUE),ncol=3) M</pre><p>On executing, the above script generates the below output(this output will vary on your system due to randomization) &minus;</p><pre class="result notranslate">&nbsp; &nbsp; &nbsp;[,1] [,2] [,3] [1,] &nbsp;9 &nbsp; &nbsp;5 &nbsp; &nbsp; 6 [2,] &nbsp;10 &nbsp; 2 &nbsp; &nbsp; 5 [3,] &nbsp;4 &nbsp; &nbsp;7 &nbsp; &nbsp; 5 [4,] &nbsp;10 &nbsp; 1 &nbsp; &nbsp; 4 [5,] &nbsp;5 &nbsp; &nbsp;1 &nbsp; &nbsp; 4 [6,] &nbsp;8 &nbsp; &nbsp;3 &nbsp; &nbsp; 1 [7,] &nbsp;4 &nbsp; &nbsp;10 &nbsp; &nbsp;10 [8,] &nbsp;4 &nbsp; &nbsp;4 &nbsp; &nbsp; 9 [9,] &nbsp;6 &nbsp; &nbsp;9 &nbsp; &nbsp; 7 [10,] 2 &nbsp; &nbsp;7 &nbsp; &nbsp; 7 [11,] 9 &nbsp; &nbsp;5 &nbsp; &nbsp; 4 [12,] 10 &nbsp; 10 &nbsp; 2 [13,] 6 &nbsp; &nbsp;2 &nbsp; &nbsp;3 [14,] 10 &nbsp; 6 &nbsp; &nbsp;8 [15,] 9 &nbsp; &nbsp;7 &nbsp; &nbsp;6 [16,] 7 &nbsp; &nbsp;5 &nbsp; &nbsp;8 [17,] 1 &nbsp; &nbsp;8 &nbsp; &nbsp;10 [18,] 3 &nbsp; &nbsp;5 &nbsp; &nbsp;1 [19,] 4 3 5 [20,] 4 3 10 [21,] 5 5 6 [22,] 6 7 10 [23,] 8 1 9 [24,] 3 9 10 [25,] 4 6 10</pre><h2>Divide the matrix row values by row maximum</h2><p>Using apply function to divide the row values of M by row maximum &minus;</p><p><a class="demo" href="http://tpcg.io/hbuuQoPp " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">M&lt;-matrix(sample(1:10,75,replace=TRUE),ncol=3) M_new&lt;-t(apply(M,1, function(x) x/max(x))) M_new</pre><h3>Output</h3><pre class="result notranslate">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[,1] &nbsp; &nbsp; &nbsp;[,2] &nbsp; &nbsp;[,3] [1,] 1.0000000 0.5555556 0.6666667 [2,] 1.0000000 0.2000000 0.5000000 [3,] 0.5714286 1.0000000 0.7142857 [4,] 1.0000000 0.1000000 0.4000000 [5,] 1.0000000 0.2000000 0.8000000 [6,] 1.0000000 0.3750000 0.1250000 [7,] 0.4000000 1.0000000 1.0000000 [8,] 0.4444444 0.4444444 1.0000000 [9,] 0.6666667 1.0000000 0.7777778 [10,] 0.2857143 1.0000000 1.0000000 [11,] 1.0000000 0.5555556 0.4444444 [12,] 1.0000000 1.0000000 0.2000000 [13,] 1.0000000 0.3333333 0.5000000 [14,] 1.0000000 0.6000000 0.8000000 [15,] 1.0000000 0.7777778 0.6666667 [16,] 0.8750000 0.6250000 1.0000000 [17,] 0.1000000 0.8000000 1.0000000 [18,] 0.6000000 1.0000000 0.2000000 [19,] 0.8000000 0.6000000 1.0000000 [20,] 0.4000000 0.3000000 1.0000000 [21,] 0.8333333 0.8333333 1.0000000 [22,] 0.6000000 0.7000000 1.0000000 [23,] 0.8888889 0.1111111 1.0000000 [24,] 0.3000000 0.9000000 1.0000000 [25,] 0.4000000 0.6000000 1.0000000</pre>
raja
Updated on 13-Aug-2021 11:09:01

Advertisements