How to find the difference between row values starting from bottom of an R data frame?

R ProgrammingServer Side ProgrammingProgramming

<p>If an R data frame contains all numerical columns and we want to find the difference between row values then we will lose first row of the data frame because that will not be subtracted from any row. This can be done by using head function and minus sign. It will work as subtracting the second last row from the last row, then subtracting third last row from the second last row and so on.</p><h2>Example</h2><p>Consider the below data frame &minus;</p><p><a class="demo" href="http://tpcg.io/iBTQPCqR" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">x1&lt;-rpois(20,2) y1&lt;-rpois(20,3) z1&lt;-rpois(20,5) w1&lt;-rpois(20,8) v1&lt;-rpois(20,10) df1&lt;-data.frame(x1,y1,z1,w1,v1) df1</pre><h2>Output</h2><pre class="result notranslate">&nbsp; x1 y1 z1 w1 v1 1 &nbsp;2 &nbsp;3 &nbsp;3 13 15 2 &nbsp;4 &nbsp;1 &nbsp;8 12 &nbsp;7 3 &nbsp;1 &nbsp;1 &nbsp;5 &nbsp;8 &nbsp;8 4 &nbsp;2 &nbsp;2 &nbsp;5 &nbsp;6 11 5 &nbsp;4 &nbsp;4 &nbsp;5 &nbsp;7 &nbsp;8 6 &nbsp;4 &nbsp;9 &nbsp;4 &nbsp;8 &nbsp;6 7 &nbsp;1 &nbsp;2 &nbsp;4 10 12 8 &nbsp;1 &nbsp;3 &nbsp;4 10 &nbsp;9 9 &nbsp;2 &nbsp;2 &nbsp;3 &nbsp;7 &nbsp;6 10 2 &nbsp;5 &nbsp;6 &nbsp;8 10 11 1 &nbsp;2 &nbsp;3 &nbsp;4 &nbsp;8 12 0 &nbsp;2 &nbsp;4 12 11 13 3 &nbsp;5 &nbsp;5 &nbsp;7 &nbsp;5 14 1 &nbsp;2 &nbsp;3 &nbsp;4 &nbsp;7 15 1 &nbsp;3 &nbsp;6 &nbsp;6 &nbsp;9 16 3 &nbsp;6 &nbsp;4 12 10 17 3 &nbsp;1 &nbsp;3 14 17 18 4 &nbsp;4 &nbsp;4 &nbsp;7 11 19 2 &nbsp;3 &nbsp;7 &nbsp;6 14 20 3 &nbsp;4 &nbsp;4 &nbsp;9 15</pre><p>Finding the difference between starting from bottom &minus;</p><h2>Example</h2><pre class="prettyprint notranslate">tail(df1,-1)-head(df1,-1)</pre><h2>Output</h2><pre class="result notranslate">&nbsp; &nbsp;x1 &nbsp;y1 &nbsp;z1 &nbsp;w1 v1 2 &nbsp; 2 &nbsp;-2 &nbsp; 5 &nbsp;-1 -8 3 &nbsp;-3 &nbsp; 0 &nbsp;-3 -4 &nbsp; 1 4 &nbsp; 1 &nbsp; 1 &nbsp; 0 &nbsp;-2 &nbsp;3 5 &nbsp; 2 &nbsp; 2 &nbsp; 0 &nbsp; 1 &nbsp;-3 6 &nbsp; 0 &nbsp; 5 &nbsp; -1 &nbsp;1 &nbsp;-2 7 &nbsp;-3 &nbsp;-7 &nbsp; &nbsp;0 &nbsp;2 &nbsp; 6 8 &nbsp; 0 &nbsp; 1 &nbsp; &nbsp;0 &nbsp;0 &nbsp;-3 9 &nbsp; 1 &nbsp; -1 &nbsp;-1 -3 &nbsp;-3 10 &nbsp;0 &nbsp; &nbsp;3 &nbsp; 3 &nbsp;1 &nbsp; 4 11 -1 &nbsp;-3 &nbsp; -3 &nbsp;-4 -2 12 -1 &nbsp; 0 &nbsp; &nbsp;1 &nbsp; 8 &nbsp;3 13 &nbsp;3 &nbsp; 3 &nbsp; &nbsp;1 &nbsp;-5 &nbsp;-6 14 &nbsp;-2 -3 &nbsp; &nbsp;-2 -3 &nbsp;2 15 &nbsp; 0 &nbsp;1 &nbsp; &nbsp;3 &nbsp; 2 &nbsp;2 16 &nbsp; 2 &nbsp;3 &nbsp; &nbsp;-2 &nbsp;6 &nbsp;1 17 &nbsp; 0 -5 &nbsp; &nbsp;-1 &nbsp;2 &nbsp;7 18 &nbsp; 1 &nbsp;3 &nbsp; &nbsp;1 &nbsp;-7 &nbsp;-6 19 &nbsp;-2 -1 &nbsp; &nbsp;3 &nbsp;-1 &nbsp;3 20 &nbsp; 1 &nbsp;1 &nbsp; &nbsp;-3 &nbsp;3 &nbsp;1</pre><p>Let&rsquo;s have a look at another example &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/cSZ1X8eT" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">x2&lt;-sample(1:10,20,replace=TRUE) y2&lt;-sample(1:10,20,replace=TRUE) z2&lt;-sample(1:10,20,replace=TRUE) w2&lt;-sample(1:10,20,replace=TRUE) v2&lt;-sample(1:10,20,replace=TRUE) df2&lt;-data.frame(x2,y2,z2,w2,v2) df2</pre><h2>Output</h2><pre class="result notranslate">&nbsp; x2 y2 z2 w2 v2 1 10 &nbsp;6 2 &nbsp;8 &nbsp;8 2 8 &nbsp; 7 1 &nbsp;6 &nbsp;10 3 6 &nbsp; 5 10 2 &nbsp;8 4 3 &nbsp; 5 4 &nbsp;4 &nbsp;4 5 10 &nbsp;6 3 &nbsp;1 &nbsp;3 6 5 &nbsp; 6 6 &nbsp;7 &nbsp;1 7 4 &nbsp; 7 5 &nbsp;9 &nbsp;4 8 2 &nbsp;6 &nbsp;2 &nbsp;1 1 9 2 &nbsp;7 &nbsp;5 &nbsp;4 4 10 2 2 &nbsp;6 &nbsp;6 9 11 2 2 &nbsp;4 &nbsp;8 3 12 2 10 1 &nbsp;5 2 13 8 3 &nbsp;8 &nbsp;8 10 14 8 9 &nbsp;3 &nbsp;10 3 15 7 5 10 &nbsp;2 &nbsp;1 16 2 8 &nbsp;4 &nbsp;7 &nbsp;5 17 4 3 &nbsp;7 &nbsp;4 &nbsp;2 18 5 8 &nbsp;3 &nbsp;8 &nbsp;2 19 5 9 &nbsp;5 &nbsp;4 &nbsp;2 20 6 5 &nbsp;6 &nbsp;7 &nbsp;9</pre><h2>Example</h2><pre class="prettyprint notranslate">tail(df2,-1)-head(df2,-1)</pre><h2>Output</h2><pre class="result notranslate">&nbsp; x2 y2 z2 w2 v2 2 -2 1 -1 -2 &nbsp;2 3 -2 -2 9 -4 -2 4 -3 0 -6 &nbsp;2 -4 5 &nbsp;7 1 -1 -3 -1 6 -5 0 &nbsp;3 &nbsp;6 -2 7 -1 1 -1 &nbsp;2 &nbsp;3 8 -2 -1 -3 -8 -3 9 &nbsp;0 &nbsp;1 &nbsp;3 &nbsp;3 &nbsp;3 10 0 -5 &nbsp;1 &nbsp;2 &nbsp;5 11 0 0 &nbsp;-2 &nbsp;2 -6 12 0 8 &nbsp;-3 -3 -1 13 6 -7 &nbsp;7 &nbsp;3 &nbsp;8 14 0 &nbsp;6 &nbsp;-5 &nbsp;2 -7 15 -1 -4 &nbsp;7 -8 -2 16 -5 &nbsp;3 -6 &nbsp; 5 4 17 2 &nbsp;-5 &nbsp;3 -3 -3 18 1 &nbsp;5 &nbsp;-4 &nbsp; 4 0 19 0 &nbsp;1 &nbsp;2 &nbsp;-4 &nbsp;0 20 1 -4 &nbsp;1 &nbsp; 3 &nbsp;7</pre>
raja
Updated on 08-Sep-2020 12:30:32

Advertisements