Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dividing rows of a dataset by a column value

Here i wish to divide the value in each row by the corresponding daily (each row is a day) maximum value, how would i do this? Finding it tricky as i dont want to divide the first 2 columns of the data by this. Data snipet below;

so i want to divide each of the values in each row by the max in each row but avoiding the date and substation

Date         Substation `00:00` `00:10` `00:20` `00:30` `00:40` `00:50` `01:00` `01:10` `01:20`   max
 <date>          <int>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 2013-01-03     511016   257.   259.    264.    263.    248.     239.   243.     230.   228.    264.  
 2 2013-01-03     511029    96    111.    116.     99.0   123.     128.   130.     126.   116.    130.  
 3 2013-01-03     511030   138.   129.    127.    124.    119.     126.   125.     121.   112.    138.  
 4 2013-01-03     511033   172.   165.    167.    170.    171.     173.   173.     166.   157.    173.  
 5 2013-01-03     511034   302.   298.    302.    290.    291.     287.   280.     291.   277.    302.  
 6 2013-01-03     511035   116.   131.    130.    121.    116.     108.   106.     112.   109.    131.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With pmax and do.call:

df[-c(1, 2)] / do.call(pmax, df[-c(1, 2)])

Or, you can apply a function, x / max(x), for each rows (MARGIN = 1) of your selected columns (df[-c(1, 2)]) using apply.

t(apply(df[-c(1, 2)], 1, function(x) x / max(x)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading