How to Calculate Lag by Group in R?

How to Calculate Lag by Group in R?, The dplyr package in R can be used to calculate lagged values by group using the following syntax.

Subsetting with multiple conditions in R – Data Science Tutorials

df %>%
  group_by(var1) %>%
  mutate(lag1_value = lag(var2, n=1, order_by=var1))

The data frame containing the lagged values gains a new variable as a result of the mutate() procedure.

The usage of this syntax in practice is demonstrated by the example that follows.

How to Calculate Lag by Group in R?

Assume we have the following R data frame, which displays the sales generated by two separate stores on various days.

What Is the Best Way to Filter by Date in R? – Data Science Tutorials

Let’s create a data frame

df <- data.frame(store=c('Store1', 'Store2', 'Store1', 'Store2', 'Store1', 'Store2', Store1', 'Store2'),sales=c(1057, 1212, 1560, 459, 1259, 4511, 28718, 789523))

Now we can view the data frame

df
  store  sales
1 Store1   1057
2 Store2   1212
3 Store1   1560
4 Store2    459
5 Store1   1259
6 Store2   4511
7 Store1  28718
8 Store2 789523

The new column that displays the lagged values of sales for each retailer may be made using the code below:

library(dplyr)

Let’s calculate the lagged sales by group

5 Free Books to Learn Statistics For Data Science – Data Science Tutorials

df %>%
  group_by(store) %>%
  mutate(lag1_sales = lag(sales, n=1, order_by=store))
  store   sales lag1_sales
  <chr>   <dbl>      <dbl>
1 Store1   1057         NA
2 Store2   1212         NA
3 Store1   1560       1057
4 Store2    459       1212
5 Store1   1259       1560
6 Store2   4511        459
7 Store1  28718       1259
8 Store2 789523       4511

How to interpret the result is as follows:

Due to the absence of a prior sales value for the store Store1A, the first value of lag1 sales is NA.

How to add labels at the end of each line in ggplot2? (datasciencetut.com)

Due to the absence of a previous sales value for store 2, the second value of lag1 sales is NA.

Because 1057 was store 1’s prior sales figure, it is the third value of lag1 sales.

Due to store 2’s prior sales value of 1212, the fourth value of lag1 sales is 1212.

so forth.

Tips for Rearranging Columns in R – Data Science Tutorials

Keep in mind that by altering the value for n in the lag() method, you can also adjust the number of lags that are used.

You may also like...

Leave a Reply

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

ten + 3 =