Difference function in R -diff() returns suitably lagged and iterated differences. diff() function takes either vector or dataframe as input along with lag and calculates the difference. Here we also look at an example of how to find the difference of a column in a dataframe in R using diff function.

- Difference of a vector at lag 1 and lag 2 using diff() function in R
- Difference of a column in dataframe using diff() function.
- Difference between two consecutive pairs of elements of vector in R .

**Syntax for difference function in R – diff():**

**x –**numeric vector**lag-**an integer indicating how many lags to use.**Difference-**order of difference

#### Pictographic Representation of difference function

**LAG 1 & Diff 1**

**LAG 2 & Diff 1:**

#### Example of difference function in R with lag 1:

#difference function in R diff(c(2,3,5,18,4,6,4),lag=1)

diff() with lag=1 calculates difference between 2^{nd} element and 1^{st} element and then difference between 3^{rd} element and 2^{nd} element and so on**. **So the output will be

**output: **

** **

#### Example of difference function in R with lag 2:

#difference function in R with lag=2 diff(c(2,3,5,18,4,6,4),lag=2)

diff() with lag=2 calculates difference between 3^{rd} element and 1^{st} element and then difference between 4^{th} element and 2^{nd} element and so on**. **So the output will be

**output: **

** **

#### Example of difference function with lag 1 and differences 2:

#difference function in R with lag=1 and differences=2 diff(c(2,3,5,18,4,6,4),lag=1,differences=2)

First it is differenced with lag=1 and the result is again differenced with lag=1

So the output will be

#### Difference of a column using diff() function

Difference of a column is accomplished using diff() function. First lets create the dataframe.

## Create dataframe df = data.frame (NAME =c ('Alisa','Bobby','jodha','jack','raghu','Cathrine', 'Alisa','Bobby','kumar','Alisa','jack','Cathrine'), Age = c (26,24,26,22,23,24,26,24,22,26,22,25), Score =c(85,63,55,74,31,77,85,63,42,85,74,78)) df

so the created dataframe df will be.

library(data.table) DT = data.table(df) DT[ , list(NAME,Age,Score,Age_Diff=diff(Age)) ]

First we convert the dataframe to data table and then use diff() function in R. diff() calculates difference between 2^{nd} element and 1^{st} element and then difference between 3^{rd} element and 2^{nd} element of “Age” column and so on**. **So the output will be