Function

コメント・参照等
  • 時系列データのラグ系列を作成する関数

FUN = f_add_lag_series.r
function (df0, col_date = 1, col_data = 2, lag = 1, diff = 1) 
{
    df1 <- na.omit(df0[, c(col_date, col_data)])
    lagdf <- data.frame(Date = tail(df1[, 1], -(lag * diff)), Diff = diff(df1[, 2], lag = lag, diff = diff), stringsAsFactors = F, row.names = NULL)
    colnames(lagdf)[2] <- paste0(colnames(df1)[2], ":lag=", lag)
    df <- merge(df1, lagdf, all = T)
    return(df)
}