title: R: Error in xts - order.by
body: <p>I am trying to (re)build a basic prediction model of the S&amp;P 500 INDEX (data orignates from Yahoo finance)</p>
  
  <p>I ran into some difficulties with the "ordering" of my data set.<br>
  During the build of data.model the following error occurs</p>
  
  <p><strong>Error in xts(new.x, x.index) : NROW(x) must match length(order.by)</strong></p>
  
  <p>After some research I realize that the problem is with the ordering, and it seems to lack ordering as is required for the underlying zoo package. </p>
  
  <p>Is there an elegant way to solve this issue?!  Thanks in advance</p>
  
  <pre><code>library(xts)
  library(tseries)
  library(quantmod)
  
  GSPC &lt;- as.xts(get.hist.quote("^GSPC",start="1970-01-02", 
  quote=c("Open", "High", "Low", "Close","Volume","AdjClose")))
  
  head(GSPC)
  
  T.ind &lt;- function(quotes, tgt.margin = 0.025, n.days = 10) {
   v &lt;- apply(HLC(quotes), 1, mean)
   r &lt;- matrix(NA, ncol = n.days, nrow = NROW(quotes))
   for (x in 1:n.days) r[, x] &lt;- Next(Delt(v, k = x), x)
   x &lt;- apply(r, 1, function(x) sum(x[x &gt; tgt.margin | x &lt;
   -tgt.margin]))
   if (is.xts(quotes))
   xts(x, time(quotes))
   else x
  }
  
  
  myATR &lt;- function(x) ATR(HLC(x))[, "atr"]
  mySMI &lt;- function(x) SMI(HLC(x))[, "SMI"]
  myADX &lt;- function(x) ADX(HLC(x))[, "ADX"]
  myAroon &lt;- function(x) aroon(x[, c("High", "Low")])$oscillator
  myBB &lt;- function(x) BBands(HLC(x))[, "pctB"]
  myChaikinVol &lt;- function(x) Delt(chaikinVolatility(x[, c("High", "Low")]))[, 1]
  myCLV &lt;- function(x) EMA(CLV(HLC(x)))[, 1]
  myEMV &lt;- function(x) EMV(x[, c("High", "Low")], x[, "Volume"])[, 2]
  myMACD &lt;- function(x) MACD(Cl(x))[, 2]
  myMFI &lt;- function(x) MFI(x[, c("High", "Low", "Close")], x[, "Volume"])
  mySAR &lt;- function(x) SAR(x[, c("High", "Close")])[, 1]
  myVolat &lt;- function(x) volatility(OHLC(x), calc = "garman")[, 1]
  
  library(randomForest)
  data.model &lt;- specifyModel(T.ind(GSPC) ~ Delt(Cl(GSPC),k=1:10) +
   myATR(GSPC) + mySMI(GSPC) + myADX(GSPC) + myAroon(GSPC) +
   myBB(GSPC) + myChaikinVol(GSPC) + myCLV(GSPC) +
   CMO(Cl(GSPC)) + EMA(Delt(Cl(GSPC))) + myEMV(GSPC) +
   myVolat(GSPC) + myMACD(GSPC) + myMFI(GSPC) + RSI(Cl(GSPC)) +
   mySAR(GSPC) + runMean(Cl(GSPC)) + runSD(Cl(GSPC)))
  </code></pre>