How do I eliminate the first variable for testing with the t-1 variable?

Asked

Viewed 67 times

2

I import the Excel files but I can’t lag the variable in R. The lag function it eliminates the first observation and I can’t use nrow or anything like that. How would I do it? I wanted to use it to take the Berenblutt-Webb test

  • Put the code you used to read the spreadsheet?

1 answer

1

Try the function lag package dplyr. She has an argument default which can be used to complete the previous values. In this case I used NA.

library(dplyr)

df <- data.frame(
  dia = seq(from = as.Date("2017-01-01"), length.out = 100, by = 1),
  x = 1:100
)


df <- df %>%
  mutate(
    x_lag = lag(x, n = 1, default = NA)
  )

head(df)


         dia x x_lag
1 2017-01-01 1    NA
2 2017-01-02 2     1
3 2017-01-03 3     2
4 2017-01-04 4     3
5 2017-01-05 5     4
6 2017-01-06 6     5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.