Home > Net >  Generate new column as difference of nth and n-1th elements
Generate new column as difference of nth and n-1th elements

Time:06-14

Hello I have the following dataset:

year<-c(0,1,2,3,4,5,6,7,8)
total<-c(4462,8233,8233,8631,8631,9016,9016,9420,9494)
df<-data.frame(year,total)

I need to compute a third column from the difference of n and n-1 elements in the second column:

new<-c(4462,3771,0,398,0,385,0,404,74)

So that the result for the first element is for instance 8233-4462 = 3771; second element 8233 - 8233 = 0, etc.

How could I do this? The final result should be:

df<-data.frame(year,total,new)

CodePudding user response:

You can use diff

> transform(df, new = diff(c(0,total)))
  year total  new
1    0  4462 4462
2    1  8233 3771
3    2  8233    0
4    3  8631  398
5    4  8631    0
6    5  9016  385
7    6  9016    0
8    7  9420  404
9    8  9494   74

CodePudding user response:

dplyr option using lag:

library(dplyr)
df %>%
  mutate(new = (total-lag(total)),
         new = coalesce(new, total))

Output:

  year total  new
1    0  4462 4462
2    1  8233 3771
3    2  8233    0
4    3  8631  398
5    4  8631    0
6    5  9016  385
7    6  9016    0
8    7  9420  404
9    8  9494   74

CodePudding user response:

We could use lag()

library(dplyr)

df %>% 
  mutate(new = total-lag(total, default = total[1] total[1]*-1))
  year total  new
1    0  4462 4462
2    1  8233 3771
3    2  8233    0
4    3  8631  398
5    4  8631    0
6    5  9016  385
7    6  9016    0
8    7  9420  404
9    8  9494   74
  •  Tags:  
  • r
  • Related