• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

BuildACloud

All Programming Languages

  • R
  • Typescript
  • JavaScript
  • Python

The diff() function in R

December 10, 2022 by cloudmin Leave a Comment

If you want to find the difference of elements in a sequence, instead of using a loop and calculating each difference, you can use the diff() function. This article will show you more detail about the diff() function to manipulate sequences.

What is the diff() function in R

The diff() function calculates the difference between each pair of elements in a sequence or columns in a matrix and returns a new sequence/matrix that contains the results.

Syntax: 

diff(sequence, lag, differences) 

Argument: 

sequence: A sequence of numbers

lag: Distance of index between 2 numbers. The default value is 1

differences: The number of times applying the function to the sequence. The default value is 1

How to use diff() function

In this part, we will give you a few examples of the usage of the function.

Calculate the differences in a sequence of numbers

First, we will take an example to see how the function works with a sequence.

Code:

# Declare a sequence of numbers
mySeq = c(1, 5, 1, 3, 7, 2, 4, 4)

# Calculate the differences in the sequence
diffSeq = diff(mySeq)

cat("The difference between each elements are:\n")
print(diffSeq)

Result:

The difference between each element are:
[1] 4 -4 2 4 -5 2 0

The result shows the difference between two adjacent elements. For example, 4 = 5 – 1, 4 = 1 – 5, 2 = 3 – 1, etc.

Look at another example to understand the function deeply.

Code:

# Declare a sequence of numbers
mySeq = c(1, 5, 1, 3, 7, 2, 4, 4)

# Calculate the differences in the sequence
diffSeq = diff(mySeq, lag = 3, differences = 2)

cat("The difference between each elements are:\n")
print(diffSeq)

Result:

The difference between each element are:
[1] -1 -5

In the example above, we find the difference between the ith element and the i+3 element, which is determined by the lag parameter. The ‘differences’ parameter tells the function to apply twice.

with lag = 3 and differences = 1, we get the result: 2 2 1 1 -3

When differences = 1, the function executes once more with the result sequence, and the final result is: -1, 5

Calculate the differences in a matrix

When applying the function to a matrix, the function considers each column of the matrix as a sequence. 

Code:

# Declare a vector
v = c(1, 5, 1, 3, 7, 2, 4, 4, 9)

# Create a 3x3 matrix from the vector
m = matrix(v, nrow = 3, ncol = 3)

cat("The initial matrix is:\n")
print(m)

m = diff(m)

cat("The matrix after applying the diff() function is:\n")
print(m)

Result:

The initial matrix is:
     [,1] [,2] [,3]
[1,] 1 3 4
[2,] 5 7 4
[3,] 1 2 9
The matrix after applying the diff() function is:
     [,1] [,2] [,3]
[1,] 4 4 0
[2,] -4 -5 5

Summary

In summary, the diff() function calculates the difference between a pair of elements in a sequence, while the ‘lag’ parameter can determine the index of two elements. If you want to use the function as a recursive, pass a value to the ‘differences’ parameter. 

Filed Under: R

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • dist function in R – Calculate the distance of the matrix
  • How to count in a for loop in Python
  • How To Use The cut() Function In R
  • How To Resolve The TypeError: unhashable type: ‘dict’ in Python
  • The diff() function in R

Recent Comments

No comments to show.

Copyright © 2023 · Metro Pro on Genesis Framework · WordPress · Log in