如何替换 `xts` 数据的特定日期的最高价?

How to replace a particular date's High price of a `xts` data?

我有一个 xts 格式的 EURUSD 数据,在 2008-03-17 的最高价上出现异常值。我想用 0.7 替换这个值,但我做不对。

代码如下,数据可从here

下载
library(xts)
EURUSD_DAY <- structure(c(0.64008, 0.635, 0.63504, 0.64354, 13.717, 0.64033, 
  0.63767, 0.62881, 0.63179, 0.64029, 0.635, 0.63865, 0, 0, 0, 0.64029, 0.635,
  0.63865), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date",
  .indexTZ = "UTC", tzone = "UTC", index = structure(c(1205452800, 1205712000,
  1205798400), tzone = "UTC", tclass = "Date"), .Dim = c(3L, 6L),
  .Dimnames = list(NULL, c("EURUSD_DAY.Open", "EURUSD_DAY.High", 
  "EURUSD_DAY.Low", "EURUSD_DAY.Close", "EURUSD_DAY.Volume",
  "EURUSD_DAY.Adjusted")))
#EURUSD_DAY <- as.xts(read.zoo("EURUSD_DAY.csv", sep=",", header=TRUE))
EURUSD_DAY["2008-03-17"]
           EURUSD_DAY.Open EURUSD_DAY.High EURUSD_DAY.Low
2008-03-17           0.635          13.717        0.62881
           EURUSD_DAY.Close EURUSD_DAY.Volume EURUSD_DAY.Adjusted
2008-03-17            0.635                 0               0.635

我尝试了以下代码来替换 EURUSD_DAY.High 值。但它没有用:

> a <- coredata(EURUSD_DAY["2008-03-17"])
> a
     EURUSD_DAY.Open EURUSD_DAY.High EURUSD_DAY.Low
[1,]           0.635          13.717        0.62881
     EURUSD_DAY.Close EURUSD_DAY.Volume EURUSD_DAY.Adjusted
[1,]            0.635                 0               0.635
> a[1,2]
EURUSD_DAY.High 
         13.717 
> a[1,2] <- 0.7
> a[1,2]
EURUSD_DAY.High 
            0.7 
> coredata(EURUSD_DAY["2008-03-17"])[1,2] <- 0.7
Warning message:
In NextMethod(.Generic) :
  number of items to replace is not a multiple of replacement length
> EURUSD_DAY["2008-03-17"]
           EURUSD_DAY.Open EURUSD_DAY.High EURUSD_DAY.Low
2008-03-17           0.635          13.717        0.62881
           EURUSD_DAY.Close EURUSD_DAY.Volume
2008-03-17            0.635                 0
           EURUSD_DAY.Adjusted
2008-03-17               0.635

我不确定您要使用 coredata... 做什么,但正确的方法是对原始对象使用常规子集函数,并在中指定行和列电话。

R> EURUSD_DAY["2008-03-17", "EURUSD_DAY.High"] <- 0.7
R> EURUSD_DAY
           EURUSD_DAY.Open EURUSD_DAY.High EURUSD_DAY.Low EURUSD_DAY.Close
2008-03-14         0.64008         0.64354        0.63767          0.64029
2008-03-17         0.63500         0.70000        0.62881          0.63500
2008-03-18         0.63504         0.64033        0.63179          0.63865
           EURUSD_DAY.Volume EURUSD_DAY.Adjusted
2008-03-14                 0             0.64029
2008-03-17                 0             0.63500
2008-03-18                 0             0.63865