How to Use diff Function in R (With Examples)


You can use the diff() function in R to calculate lagged differences between consecutive elements in vectors.

diff(x)

The following examples show how to use this function in practice.

Example 1: Find Lagged Differences Between Consecutive Elements

The following code shows how to find the lagged differences between elements in a vector:

#define vector
x <- c(4, 6, 9, 8, 13)

#find lagged differences between consecutive elements
diff(x)

[1]  2  3 -1  5

Here is how the lagged differences were calculated:

  • 6 – 4 = 2
  • 9 – 6 = 3
  • 8 – 9 = -1
  • 13 – 8 = 5

Example 2: Find Lagged Differences Between Non-Consecutive Elements

The following code shows how to use the lag argument to find the lagged differences between elements that are 2 positions apart in a vector:

#define vector
x <- c(4, 6, 9, 8, 13)

#find lagged differences between elements 2 positions apart
diff(x, lag=2)

[1] 5 2 4

Here is how the lagged differences were calculated:

  • 9 – 4 = 5
  • 8 – 6 = 2
  • 13 – 9 = 4

Example 3: Find Lagged Differences in Column of Data Frame

The following code shows how to find the lagged differences between a specific column in a data frame:

#define data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 9))

#view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3    7    3    1
3    3    8    6    2
4    4    3    6    8
5    5    2    8    9

#find lagged differences between elements in 'var1' column
diff(df$var1)

[1] 2 0 1 1

Example 4: Find Lagged Differences in Several Columns of Data Frame

The following code shows how to use the sapply() function to find the lagged differences between several columns in a data frame:

#define data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 9))

#view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3    7    3    1
3    3    8    6    2
4    4    3    6    8
5    5    2    8    9

#find lagged differences between elements in each column
sapply(df, diff)

     var1 var2 var3 var4
[1,]    2    0    0    0
[2,]    0    1    3    1
[3,]    1   -5    0    6
[4,]    1   -1    2    1

Additional Resources

A Guide to apply(), lapply(), sapply(), and tapply() in R
How to Apply Function to Each Row in Matrix or Data Frame in R

Leave a Reply

Your email address will not be published.