Calculate the difference between Consecutive pair of Elements of a Vector in R Programming - diff() Function Last Updated : 05 Jun, 2020 Comments Improve Suggest changes Like Article Like Report diff() function in R Language is used to find the difference between each consecutive pair of elements of a vector. Syntax: diff(x, lag, differences) Parameters: x: vector or matrix lag: period between elements differences: Order of difference Example 1: Python3 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) 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: Python3 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) # Calling diff() function diff(x1, lag = 2, differences = 1) diff(x2, lag = 1, differences = 2) 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. Comment More infoAdvertise with us Next Article Calculate the difference between Consecutive pair of Elements of a Vector in R Programming - diff() Function N nidhi_biet Follow Improve Article Tags : R Language R Vector-Function Similar Reads Calculate Time Difference between Dates in R Programming - difftime() Function difftime() function in R Language is used to calculate time difference between dates in the required units like seconds, minutes, days, weeks, etc. Syntax: difftime(date1, date2, units) Parameters:date1, date2: Dates to calculate difference units: seconds, minutes, days, weeksExample 1:R# R program 1 min read Find the Difference Between Two Dates in R Programming - julian() Function In R programming, difference between two dates can be determined by using basic function julian() and passing date object as the parameter in the function. It ignores the leap-seconds. Syntax: julian(x, origin) Parameters: x: represents date object origin: represents another date object from where d 1 min read Creating a Vector of sequenced elements in R Programming - seq() Function In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct 2 min read Get Exclusive Elements between Two Objects in R Programming - setdiff() Function setdiff() function in R Programming Language is used to find the elements which are in the first Object but not in the second Object. Syntax: setdiff(x, y) Parameters:Â x and y: Objects with sequence of itemsR - setdiff() Function ExampleExample 1: Apply setdiff to Numeric Vectors in R LanguageR # R 2 min read How to Create, Access, and Modify Vector Elements in R ? In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements. Creating a vectorIt can be done in these ways: Using c() function.Using: operator.Using the seq 5 min read Like