I want to place the results of a lagged diff back into my data frame. It means I would have leading NAs for the different lags.
I am using:
new.df$lag1 <- diff(new.df$Close, lag = 1, differences = 1, arithmetic = TRUE, na.pad = TRUE)
Error in `$<-.data.frame`(`*tmp*`, lag1, value = c(0.248860000000001, :
replacement has 6177 rows, data has 6178
I thought that if it said na.pad=TRUE then this would place a NA on row 1 and the lag diff on row 2. This is not the case.
heres some sample data:
data <- c(10,15,89,40,55,67,79)
lag1 <- diff(data, lag = 1, differences = 1, arithmetic = TRUE, na.pad = TRUE)
goal is to place this back into the dataframe... with leading NA's depending on number of lags.