Duck
Duck

Reputation: 39605

Compute difference between rows in R and setting in zero first difference

Hi everybody I am trying to solve a little problem in R. I want to compute the difference between rows in a dataframe in R. My dataframe looks like this:

df <- data.frame(ID=1:8, x2=8:1, x3=11:18, x4=c(2,4,10,0,1,1,9,12))

I want to create a new column named diff.var. This column saves the results of differences from rows in variable. One posibble solution is using diff() function. When I used this function I got this:

diff(df$x4)
[1]   2   6 -10   1   0   8   3

That works fine but when I try to apply in my dataframe using df$diff.var=diff(df$x4) I got this:

Error in `$<-.data.frame`(`*tmp*`, "diff.var", value = c(2, 6, -10, 1,  : 
  replacement has 7 rows, data has 8

Due to the fact that the firs row doesn't have a previous row to compute the difference I want to set this in zero. I would like to get something this:

ID  x2  x3  x4  diff.var
1   8   11  2   0
2   7   12  4   2
3   6   13  10  6
4   5   14  0   -10
5   4   15  1   1
6   3   16  1   0
7   2   17  9   8
8   1   18  12  3

Where the first element of diff.var is zero due to this element doesn't have a previous element. I would like to build a function to set firts element of diff.var is zero and that makes the differences for the next rows. I wish to create a new dataframe with all variables and diff.var because ID is used por posterior analysis with diff.var. diff() doesn't allow to create this new variable. Thanks for your help.

Upvotes: 0

Views: 6997

Answers (1)

Paulo E. Cardoso
Paulo E. Cardoso

Reputation: 5856

This question was already asked before in this forum and can be found elsewhere. Anyway, do what Frank suggests

df <- data.frame(ID=1:8, x2=8:1, x3=11:18, x4=c(2,4,10,0,1,1,9,12))
df$vardiff <- c(0, diff(df$x4))
df
  ID x2 x3 x4 vardiff
1  1  8 11  2       0
2  2  7 12  4       2
3  3  6 13 10       6
4  4  5 14  0     -10
5  5  4 15  1       1
6  6  3 16  1       0
7  7  2 17  9       8
8  8  1 18 12       3

Upvotes: 5

Related Questions