I import the Excel files but I can not get the varable in R. The lag function deletes the first comment and I can not use the nrow or something. How would I do it? I would like to use it to take the Berenblutt-Webb test
I import the Excel files but I can not get the varable in R. The lag function deletes the first comment and I can not use the nrow or something. How would I do it? I would like to use it to take the Berenblutt-Webb test
Try the lag
function of the dplyr
package. It has a default
argument that can be used to complete 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