6.9 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Calculate Difference Between Rows in R

How to Calculate Difference Between Rows in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the diff() function to calculate the difference between rows of a data frame in R:

#find difference between rows in every column of data frame
diff(as.matrix(df))

#find difference between rows of specific column
diff(df$column_name)

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

Example 1: Find Difference Between Rows of Every Column

The following code shows how to calculate the difference between rows for every column in a data frame:

#create data frame
df frame(day=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                 sales=c(7, 8, 8, 12, 10, 9, 13, 16, 11, 7))

#view data frame
df

   day sales
1    1     7
2    2     8
3    3     8
4    4    12
5    5    10
6    6     9
7    7    13
8    8    16
9    9    11
10  10     7

#calculate difference between rows for each column
diff(as.matrix(df))

      day sales
 [1,]   1     1
 [2,]   1     0
 [3,]   1     4
 [4,]   1    -2
 [5,]   1    -1
 [6,]   1     4
 [7,]   1     3
 [8,]   1    -5
 [9,]   1    -4

Example 2: Find Difference Between Rows of Specific Column

The following code shows how to calculate the difference between rows for a specific column in a data frame:

#create data frame
df frame(day=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                 sales=c(7, 8, 8, 12, 10, 9, 13, 16, 11, 7))

#calculate difference between rows in 'sales' column
diff(df$sales)

[1]  1  0  4 -2 -1  4  3 -5 -4

Example 3: Find Difference Between Rows & Append New Column

The following code shows how to calculate the difference between rows for a specific column in a data frame and then append those differences as a new column at the end of the data frame:

#create data frame
df frame(day=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                 sales=c(7, 8, 8, 12, 10, 9, 13, 16, 11, 7))

#calculate difference between rows in 'sales' column
sales_diff #append NA to beginning of differences vector
sales_diff #append differences vector as new column
df$sales_diff #view updated data frame
df

   day sales sales_diff
1    1     7         NA
2    2     8          1
3    3     8          0
4    4    12          4
5    5    10         -2
6    6     9         -1
7    7    13          4
8    8    16          3
9    9    11         -5
10  10     7         -4

Additional Resources

The following tutorials explain how to perform other common row functions in R:

How to Use colSums() Function in R
How to Use rowSums() Function in R
How to Use nrow() Function in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories