xts中如何进行单列操作

How to do single column operations in xts

我想在xts数据集上做单列操作。我有一个非常大的数据集,所以我从中删除了一些数据和日期。下面的数据集是使用 excel 计算的,只显示了开头和结尾的一些数据。

  Date      a           b        c
    -       0.023       
    -       0.021   0.0214830   
    -       0.014   0.0142940   0.021483
    -       0.008   0.0081120   0.014294
    -       0.003   0.0030240   0.008112
    -       0.002   0.0020060   0.003024
    -       0       0           0.002006
    -       0       0           0
    -       0       0           0
    -       0       0           0
    -       0.001   0.0010000   0
    -       0.003   0.0030030   0.001
    -       0.005   0.0050150   0.003003
    -       0.001   0.0010050   0.005015
    -       0       0           0.001005
    -       -0.001  -0.0010000  0
    -       0.002   0.0019980   -0.001
    -       0.003   0.0030060   0.001998
    -       .       .            .
    -       .       .            .
    -       .       .            .
    -       .       .            .
    -       .       .            .
    -       .       .            .
    -       .       .
    -       0.019   0.01938     0.02042
    -       0.015   0.0152850   0.01938
    -       0.013   0.0131950   0.015285

在上面的数据集中,a是我的初始数据,用于计算bc. b 是使用这个公式 b2=(a2*a1)+a2 (eg:(0.021*0.023)+0.021=0.021483)c 是通过将 b 向下移动一行 c3=b2.

形成的

我的问题是如何在 R xts 数据集中执行这些操作

已编辑

如何在 xts 中向上移动一列。下面的转换是 d1=a3

 Date         a       d
    -       0.023   0.014
    -       0.021   0.008
    -       0.014   0.003
    -       0.008   0.002
    -       0.003   0
    -       0.002   0
    -       0       0
    -       0       0
    -       0       0.001
    -       0       0.003
    -       0.001   0.005
    -       0.003   0.001
    -       0.005   0
    -       0.001   -0.001
    -       0       0.002
    -       -0.001  0.003
    -       0.002   0.024
    -       0.003   0.015
    -       .       .
    -       .       .
    -       .       .
    -       .       .
    -       .       .
    -       .       0.019
    -       .       0.015
    -       0.019   0.013
    -       0.015   0
    -       0.013   0

我试过了method

shift <- function(x, n){
  c(x[-(seq(n))], rep(NA, n))
}
d<-shift(df$a,2)

但是它给出了这个错误

Error in try.xts(NA) : Error in as.xts.logical(x, ..., .RECLASS = TRUE) : order.by must be either 'names()' or otherwise specified

Pascal 是正确的,您可以像在 data.frame 上操作一样进行操作。这是一个例子:

library(xts)

Ex <- xts(1:10, Sys.Date()+1:10)
names(Ex) <- "a"
Ex$b <- (Ex$a*lag(Ex$a))+Ex$a
Ex$c <- lag(Ex$b)

以上产生以下结果:

##             a   b  c
## 2015-12-25  1  NA NA
## 2015-12-26  2   4 NA
## 2015-12-27  3   9  4
## 2015-12-28  4  16  9
## 2015-12-29  5  25 16
## 2015-12-30  6  36 25
## 2015-12-31  7  49 36
## 2016-01-01  8  64 49
## 2016-01-02  9  81 64
## 2016-01-03 10 100 81

编辑:回答编辑后的问题

要在 d1=a3 中创建一个列,只需将函数中的对象更改为矩阵即可,它工作正常。也许有人可以填空为什么,但至少它现在有效。

shift <- function(x, n){
  xMat <- as.matrix(x)
  c(xMat[-(seq(n))], rep(NA, n))
}

Ex$d <- shift(Ex$a, 2)

这会产生:

##             a   b  c  d
## 2015-12-26  1  NA NA  3
## 2015-12-27  2   4 NA  4
## 2015-12-28  3   9  4  5
## 2015-12-29  4  16  9  6
## 2015-12-30  5  25 16  7
## 2015-12-31  6  36 25  8
## 2016-01-01  7  49 36  9
## 2016-01-02  8  64 49 10
## 2016-01-03  9  81 64 NA
## 2016-01-04 10 100 81 NA