合并两个xts对象时出错

时间:2012-08-23 19:28:02

标签: r xts

我无法在网上找到解决方案。两个xts对象匹配行数和列数。合并操作仍然出现以下错误 - “要替换的项目数不是替换长度的倍数”。

以下是R代码以及临时步骤中的打印输出。我对R有点新意。所以如果你注意到程序中的任何步骤可以做得更好,那么你也可以告诉我。感谢。

> # LOAD THE SPY DATA AND CREATE A DATA FRAME WITH RETURN COLUMN
> library(quantmod)
> library(PerformanceAnalytics)
> getSymbols("SPY", src='yahoo', index.class=c("POSIXt","POSIXct"), from='2002-01-01')
> SPY<-to.monthly(SPY)
> SPY.ret<-Return.calculate(SPY$SPY.Close)
> print(head(SPY.ret))
            SPY.Close
Jan 2002           NA
Feb 2002 -0.018098831
Mar 2002  0.029868840
Apr 2002 -0.059915390
May 2002 -0.005951292
Jun 2002 -0.080167070
> index(SPY.ret) = as.Date(index(SPY))  # Convert to Date format as xts index is a Date.
> colnames(SPY.ret) <- "SPY"
> print(head(SPY.ret))
                    SPY
2002-01-01           NA
2002-02-01 -0.018098831
2002-03-01  0.029868840
2002-04-01 -0.059915390
2002-05-01 -0.005951292
2002-06-01 -0.080167070

> #LOAD THE TRADE FILE & CREATE A DATA FRAME WITH PROFIT COLUMN
> trades = as.xts(read.zoo(file="Anvi/CSV/ARS_EW_R2_SPDR.csv", index.column="Exit.time", format="%m/%d/%Y", header=TRUE, sep=","))
Warning message:
In zoo(rval3, ix) :
  some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
> df = trades$Profit
> print(head(df))
           Profit        
2003-09-30 " 0.079734219"
2004-01-31 " 0.116722585"
2004-03-31 " 0.060347888"
2004-04-30 " 0.100379816"
2004-07-31 " 0.084048027"
2004-07-31 " 0.018710103"
> df$Profits = as.numeric(trades$Profit)
> df = df$Profit #Inefficent way to convert Profit column to numeric?
> print(head(df))
               Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.08404803
2004-07-31 0.01871010
> df = aggregate(df, by=index(df))
> colnames(df) = "Profit"
> print(head(df))
               Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.10275813
2004-11-30 0.02533904
> 
> #MERGE THE SPY RET AND TRADE RESULTS DATA FRAMES
> temp = head(df)
> temp1 = head(SPY.ret)
> print(temp)
               Profit
2003-09-30 0.07973422
2004-01-31 0.11672259
2004-03-31 0.06034789
2004-04-30 0.10037982
2004-07-31 0.10275813
2004-11-30 0.02533904
> print(temp1)
                    SPY
2002-01-01           NA      (Note: I tried replacing NA with 0 but still same error).
2002-02-01 -0.018098831
2002-03-01  0.029868840
2002-04-01 -0.059915390
2002-05-01 -0.005951292
2002-06-01 -0.080167070
> mdf = merge(x=temp, y=temp1, all=TRUE)
Error in z[match0(index(a), indexes), ] <- a[match0(indexes, index(a)),  : 
  number of items to replace is not a multiple of replacement length
> 

我上面要做的是合并对象,使得结果对象的索引是UNION,并且有两列“SPY”,“PROFIT”。合并对象中每个列中的空单元格用0填充。

1 个答案:

答案 0 :(得分:4)

aggregate返回一个zoo对象,而不是xts对象。这意味着正在调度merge的zoo方法而不是xts方法。如果两个对象都是xts对象,则代码可以正常工作。

temp <- 
  structure(c(0.07973422, 0.11672259, 0.06034789, 0.10037982, 0.10275813, 
  0.02533904), .Dim = c(6L, 1L), index = structure(c(12325, 12448, 
  12508, 12538, 12630, 12752), class = "Date"), class = "zoo",
 .Dimnames = list(NULL, "Profit"))
temp1 <-
  structure(c(NA, -0.018098831, 0.02986884, -0.05991539, -0.005951292, 
  -0.08016707), .Dim = c(6L, 1L), index = structure(c(1009864800, 
  1012543200, 1014962400, 1017640800, 1020229200, 1022907600), tzone = "",
  tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "",
  tzone = "", .Dimnames = list(NULL, "SPY"), class = c("xts", "zoo"))
merge(temp, temp1)  # error
merge(as.xts(temp), temp1, fill=0)  # works, filled with zeros
#                Profit          SPY
# 2002-01-01 0.00000000           NA
# 2002-02-01 0.00000000 -0.018098831
# 2002-03-01 0.00000000  0.029868840
# 2002-04-01 0.00000000 -0.059915390
# 2002-05-01 0.00000000 -0.005951292
# 2002-06-01 0.00000000 -0.080167070
# 2003-09-30 0.07973422  0.000000000
# 2004-01-31 0.11672259  0.000000000
# 2004-03-31 0.06034789  0.000000000
# 2004-04-30 0.10037982  0.000000000
# 2004-07-31 0.10275813  0.000000000
# 2004-11-30 0.02533904  0.000000000