I would like to create a new variable sales.T.minus.1 which will present the value of sales from year - 1. I created a replicable example below:
library(plm)
library(dplyr)
data <- pdata.frame(Cigar)
data("Cigar")
data <- data %>%
mutate(sales.T.minus.1 = lag(sales, 1))
Unfortunately this code is not working. The value in variable sales.T.minus.1 is the same as in variable sales.
| year | sales | sales.T.minus.1 |
|---|---|---|
| 64 | 95.4 | 95.4 |
How can I achieve the result I want?
The result I want is:
| year | sales | sales.T.minus.1 |
|---|---|---|
| 64 | 95.4 | 93.9 |
| 65 | 98.5 | 95.4 |
state.mutate(sales.T.minus.1 = lag(sales, 2), .by = state). Also, first load the data, then coerce to class'pdata.frame'. (wrong instructions order in your code.)