Missing Data Delay

I have a state approval rating data set. I need to postpone one of the variables for two years.

The data are annual and cover the period from 1970 to 2008. Obviously, if I lag behind the data, I will lose some observations (i.e. 1970 cannot find the 1968 data). I am fine with the loss of these observations, but the diff command returns an error when I try to lag.

However, when I run the delay, I get the following error: the replacement does not match the data:

> df$lagvar <- diff(df$var, lag=2)
Error in `$<-.data.frame`(`*tmp*`, "lagvar", value = c(-0.4262501,  : 
replacement has 230 rows, data has 232

I searched but cannot find a solution. Any ideas on how to get around this?

+5
source share
1 answer

diffby default does not fit with the master NA. You must add them yourself.

df$lagvar <- c(NA, NA, diff(df$var, lag=2))

, . - , :

mydiff <- function(x, ...) {
  d <- diff(x, ...)
  c(rep(NA, NROW(x)-NROW(d)), d)
}
+6

All Articles