R矢量/数据帧滞后的相反功能是什么?

时间:2021-02-22 15:45:01

I have a problem dealing with time series in R.

我在处理R中的时间序列时遇到问题

#--------------read data

wb = loadWorkbook("Countries_Europe_Prices.xlsx") 
df = readWorksheet(wb, sheet="Sheet2")

x <- df$Year
y <- df$Index1

y <- lag(y, 1, na.pad = TRUE)
cbind(x, y)

It gives me the following output:

它给了我以下输出:

        x     y
 [1,] 1974    NA
 [2,] 1975  50.8
 [3,] 1976  51.9
 [4,] 1977  54.8
 [5,] 1978  58.8
 [6,] 1979  64.0
 [7,] 1980  68.8
 [8,] 1981  73.6
 [9,] 1982  74.3
[10,] 1983  74.5
[11,] 1984  72.9
[12,] 1985  72.1
[13,] 1986  72.3
[14,] 1987  71.7
[15,] 1988  72.9
[16,] 1989  75.3
[17,] 1990  81.2
[18,] 1991  84.3
[19,] 1992  87.2
[20,] 1993  90.1

But I want the first value in y to be 50.8 and so forth. In other words, I want to get a negative lag. I don't get it, how can I do it?

但我希望y中的第一个值为50.8,依此类推。换句话说,我希望得到一个负滞后。我不明白,我该怎么做?

My problem is very similar to this problem, but however I cannot solve it. I guess I still do not understand the solution(s)...

我的问题与这个问题非常相似,但是我无法解决它。我想我仍然不明白解决方案......

Basic lag in R vector/dataframe

R向量/数据帧的基本滞后

2 个解决方案

#1


9  

How about the built-in 'lead' function? (from the dplyr package) Doesn't it do exactly the job of Ahmed's function?

内置的“引导”功能怎么样? (来自dplyr包)它不完全是Ahmed的功能吗?

cbind(x, lead(y, 1))

If you want to be able to calculate either positive or negative lags in the same function, i suggest a 'shorter' version of his 'shift' function:

如果你想在同一个函数中计算正滞后或负滞后,我建议他''shift'函数的“更短”版本:

shift = function(x, lag) {
  require(dplyr)
  switch(sign(lag)/2+1.5, lead(x, abs(lag)), lag(x, abs(lag)))
}

What it does is creating 2 cases, one with lag the other with lead, and chooses one case depending on the sign of your lag (the +1.5 is a trick to transform a {-1, +1} into a {1, 2} alternative).

它的作用是创造2个案例,其中一个案例滞后于另一个案例,并根据滞后的符号选择一个案例(+1.5是将{-1,+ 1}转换为{1,2替代方案)。

#2


3  

There is an easier way of doing this which I have captured fully from this link. What I will do here is explaining what should you do in steps:

有一种更简单的方法可以完成此操作,我已从此链接中完全捕获。我将在这里做的是解释你应该在步骤中做什么:

First create the following function by running the following code:

首先通过运行以下代码创建以下函数:

shift<-function(x,shift_by){
    stopifnot(is.numeric(shift_by))
    stopifnot(is.numeric(x))

    if (length(shift_by)>1)
        return(sapply(shift_by,shift, x=x))

    out<-NULL
    abs_shift_by=abs(shift_by)
    if (shift_by > 0 )
        out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
    else if (shift_by < 0 )
        out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
    else
        out<-x
    out
}

This will create a function called shift with two arguments; one is the vector you need to operate its lag/lead and the other is number of lags/leads you need.

这将创建一个名为shift的函数,带有两个参数;一个是你需要操作其滞后/导线的矢量,另一个是你需要的滞后/导线的数量。

Example:

Suppose you have the following vector:

假设您有以下向量:

x<-seq(1:10)

x
 [1]  1  2  3  4  5  6  7  8  9 10

if you need x's first order lag

如果你需要x的第一阶滞后

shift(x,-1)
[1] NA  1  2  3  4  5  6  7  8  9 

if you need x's first order lead (negative lag)

如果你需要x的第一阶导联(负滞后)

shift(x,1)
[1]  2  3  4  5  6  7  8  9 10 NA

#1


9  

How about the built-in 'lead' function? (from the dplyr package) Doesn't it do exactly the job of Ahmed's function?

内置的“引导”功能怎么样? (来自dplyr包)它不完全是Ahmed的功能吗?

cbind(x, lead(y, 1))

If you want to be able to calculate either positive or negative lags in the same function, i suggest a 'shorter' version of his 'shift' function:

如果你想在同一个函数中计算正滞后或负滞后,我建议他''shift'函数的“更短”版本:

shift = function(x, lag) {
  require(dplyr)
  switch(sign(lag)/2+1.5, lead(x, abs(lag)), lag(x, abs(lag)))
}

What it does is creating 2 cases, one with lag the other with lead, and chooses one case depending on the sign of your lag (the +1.5 is a trick to transform a {-1, +1} into a {1, 2} alternative).

它的作用是创造2个案例,其中一个案例滞后于另一个案例,并根据滞后的符号选择一个案例(+1.5是将{-1,+ 1}转换为{1,2替代方案)。

#2


3  

There is an easier way of doing this which I have captured fully from this link. What I will do here is explaining what should you do in steps:

有一种更简单的方法可以完成此操作,我已从此链接中完全捕获。我将在这里做的是解释你应该在步骤中做什么:

First create the following function by running the following code:

首先通过运行以下代码创建以下函数:

shift<-function(x,shift_by){
    stopifnot(is.numeric(shift_by))
    stopifnot(is.numeric(x))

    if (length(shift_by)>1)
        return(sapply(shift_by,shift, x=x))

    out<-NULL
    abs_shift_by=abs(shift_by)
    if (shift_by > 0 )
        out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
    else if (shift_by < 0 )
        out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
    else
        out<-x
    out
}

This will create a function called shift with two arguments; one is the vector you need to operate its lag/lead and the other is number of lags/leads you need.

这将创建一个名为shift的函数,带有两个参数;一个是你需要操作其滞后/导线的矢量,另一个是你需要的滞后/导线的数量。

Example:

Suppose you have the following vector:

假设您有以下向量:

x<-seq(1:10)

x
 [1]  1  2  3  4  5  6  7  8  9 10

if you need x's first order lag

如果你需要x的第一阶滞后

shift(x,-1)
[1] NA  1  2  3  4  5  6  7  8  9 

if you need x's first order lead (negative lag)

如果你需要x的第一阶导联(负滞后)

shift(x,1)
[1]  2  3  4  5  6  7  8  9 10 NA