0

The monotone decreasing data I have is

 ID  Volume      
   0  98          
   1  84          
   2  75          
   3  64          
   4  54
   5  42
   6  36
   7  25
   8   0

Now I want to mutate new columns in R, which follows a calculation from this exist column.

New1 column is the previous volume minus the current volume.

New2 column is the current volume divide the previous volume. And the last cell of the new column is always 0 instead of NA.

I want to use mutate, but do not know what should be write within it.

  ID  Volume      New1           New2
   0  98          98-84          84/98
   1  84          84-75          78/84
   2  75          75-64          64/75
   3  64          64-54          54/64
   4  54          54-42          42/54
   5  42          42-36          36/42
   6  36          36-25          25/36
   7  25          25-0           0/25
   8   0            0 
Flora
  • 463
  • 1
  • 15
  • 1
    Just change `lag()` to `lead()` to subtract next row rather than previous for. For example `yourdata %>% mutate(New1=Volume-lead(Volume, 1, 0))` – MrFlick Aug 14 '20 at 04:22
  • What if I want to do divide calculation, how can I use current value divide previous value and get value one by one? Thank you! – Flora Aug 14 '20 at 04:56
  • 1
    Just do `lead(Volume,1)/Value`. – MrFlick Aug 14 '20 at 04:58

0 Answers0