function in R Language is used to find the difference between each consecutive pair of elements of a vector.**diff()**

Syntax:diff(x, lag, differences)

Parameters:

x:vector or matrix

lag:period between elements

differences:Order of difference

**Example 1:**

`# R program to find the difference ` `# between each pair of elements of a vector ` ` ` `# Creating a vector ` `x1 <` `-` `c(` `8` `, ` `2` `, ` `5` `, ` `4` `, ` `9` `, ` `6` `, ` `54` `, ` `18` `) ` `x2 <` `-` `c(` `1` `:` `10` `) ` `x3 <` `-` `c(` `-` `1` `:` `-` `8` `) ` ` ` `# Calling diff() function ` `diff(x1) ` `diff(x2) ` `diff(x3) ` |

*chevron_right*

*filter_none*

**Output:**

[1] -6 3 -1 5 -3 48 -36 [1] 1 1 1 1 1 1 1 1 1 [1] -1 -1 -1 -1 -1 -1 -1

**Example 2:**

`# R program to find the difference ` `# between each pair of elements of a vector ` ` ` `# Creating a vector ` `x1 <` `-` `c(` `8` `, ` `2` `, ` `5` `, ` `4` `, ` `9` `, ` `6` `, ` `54` `, ` `18` `) ` `x2 <` `-` `c(` `1` `:` `10` `) ` ` ` `# Calling diff() function ` `diff(x1, lag ` `=` `2` `, differences ` `=` `1` `) ` `diff(x2, lag ` `=` `1` `, differences ` `=` `2` `) ` |

*chevron_right*

*filter_none*

**Output:**

[1] -3 2 4 2 45 12 [1] 0 0 0 0 0 0 0 0

Here, in the above code, the ‘lag’ tells the period between values, i.e. **lag = 2** means, diff is calculated between 1st and 3rd value, 2nd and 4th values, etc. and ‘differences’ tells the order in which `diff()`

function is called i.e. **differences = 2** means `diff()`

function is called twice on the vector.

## Recommended Posts:

- Calculate Median of elements of a Vector in R Programming - median() Function
- Convert an Object into a Vector in R Programming - as.vector() Function
- Check for the Existence of a Vector Object in R Programming - is.vector() Function
- Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
- Calculate Rank of the Values of a Vector in R Programming - rank() Function
- Calculate the Cumulative Minima of a Vector in R Programming - cummin() Function
- Calculate the Cumulative Maxima of a Vector in R Programming - cummax() Function
- Calculate Time Difference between Dates in R Programming - difftime() Function
- Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
- Check if Elements of a Vector are non-empty Strings in R Programming - nzchar() Function
- Make Elements of a Vector Unique in R Programming - make.unique() Function
- Replace the Elements of a Vector in R Programming - replace() Function
- Creating a Vector of sequenced elements in R Programming - seq() Function
- Check if the elements of a Vector are Finite, Infinite or NaN values in R Programming - is.finite(), is.infinite() and is.nan() Function
- Get the Sign of Elements of a Numeric Vector in R Programming - sign() Function
- Replicate elements of vector in R programming - rep() Method
- Adding elements in a vector in R programming - append() method
- Compute the gamma value of a Non-negative Numeric Vector in R Programming - gamma() Function
- Check if values in a vector are True or not in R Programming - all() and any() Function
- Getting Match of an Element within a Vector in R Programming - charmatch() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.