[R-SIG-Finance] Strange troubles with sub-setting xts

Jeff Ryan jeff.a.ryan at gmail.com
Wed Feb 23 05:31:50 CET 2011


It's likely you caught one of the interim xts R-Forge releases with a (known) bug in subsetting. 

The sessionInfo() call would show you the version in question, but the answer/fix is to update to either the latest on R-forge, or the new version 0.8-0 making its way to CRAN mirrors now. 

Best,
Jeff




On Feb 22, 2011, at 9:01 PM, Worik Stanton <worik.stanton at gmail.com> wrote:

> I cannot get a small example to illustrate this, as the problem
> disappears in strange ways.  So this email will have a lot of text.
> 
> I do not know how to get xts version but here is the R version
> 
>> R.version
>              _
> platform       i686-pc-linux-gnu
> arch           i686
> os             linux-gnu
> system         i686, linux-gnu
> status
> major          2
> minor          11.1
> year           2010
> month          05
> day            31
> svn rev        52157
> language       R
> version.string R version 2.11.1 (2010-05-31)
> 
> 
> I have a xts caled A.x
> 
>> class(A.x)
> [1] "xts" "zoo"
>> names(A.x)
> [1] "Lev"   "200MA" "RF"    "Ret"
>>> c(start(A.x), end(A.x))
> [1] "1928-01-31" "2010-07-31"
>> 
> 
> (It is a processed version of S&P500 monthly index)
> 
> When I subset A.x by date the 4th column (Ret) gets mangled.
> 
> Below are screeds of data, sorry, but if I shrink these the problem disappears.
> 
> cheers
> Worik
> 
>> Z <- merge(A.x["1929::2010",4], A.x[,4])
>> Y <- merge(Z, Z[,1]-Z[,2])
>> Y
> 1928-01-31            NA            NA            NA
> 1928-02-29            NA -1.764371e-02            NA
> 1928-03-31            NA  1.083430e-01            NA
> 1928-04-30            NA  3.240983e-02            NA
> 1928-05-31            NA  1.265823e-02            NA
> 1928-06-30            NA -4.050000e-02            NA
> 1928-07-31            NA  1.250651e-02            NA
> 1928-08-31            NA  7.411220e-02            NA
> 1928-09-30            NA  2.395783e-02            NA
> 1928-10-31            NA  1.450632e-02            NA
> 1928-11-30            NA  1.199262e-01            NA
> 1928-12-31            NA  2.883031e-03            NA
> 1929-01-31  1.720000e+00  5.708419e-02  1.662916e+00
> 1929-02-28  1.130000e+00 -5.827506e-03  1.135828e+00
> 1929-03-31  6.700000e-01 -2.344666e-03  6.723447e-01
> 1929-04-30  1.900000e-01  1.605954e-02  1.739405e-01
> 1929-05-31  3.000000e-02 -4.279106e-02  7.279106e-02
> 1929-06-30  1.300000e-01  1.123641e-01  1.763592e-02
> 1929-07-31  3.000000e-01  4.561912e-02  2.543809e-01
> 1929-08-31  2.100000e-01  9.799169e-02  1.120083e-01
> 1929-09-30  1.600000e-01 -4.888048e-02  2.088805e-01
> 1929-10-31  1.800000e-01 -1.992706e-01  3.792706e-01
> 1929-11-30  1.800000e-01 -1.337474e-01  3.137474e-01
> 1929-12-31  1.800000e-01  2.533461e-02  1.546654e-01
> 1930-01-31  1.700000e-01  6.247086e-02  1.075291e-01
> 1930-02-28  1.200000e-01  2.150066e-02  9.849934e-02
> 1930-03-31  7.000000e-02  7.989691e-02 -9.896907e-03
> 1930-04-30  5.000000e-02 -9.546539e-03  5.954654e-02
> 1930-05-31  5.000000e-02 -1.646586e-02  6.646586e-02
> 1930-06-30  6.000000e-02 -1.645570e-01  2.245570e-01
> 1930-07-31  1.100000e-01  3.665689e-02  7.334311e-02
> 1930-08-31  1.500000e-01  7.543612e-03  1.424564e-01
> 1930-09-30  1.600000e-01 -1.300889e-01  2.900889e-01
> 1930-10-31  1.600000e-01 -8.875740e-02  2.487574e-01
> 1930-11-30  1.200000e-01 -2.184179e-02  1.418418e-01
> 1930-12-31  1.600000e-01 -7.423054e-02  2.342305e-01
> 1931-01-31            NA  4.889179e-02            NA
> 1931-02-28 -1.764371e-02  1.143567e-01 -1.320005e-01
> 1931-03-31  1.083430e-01 -6.915784e-02  1.775008e-01
> 1931-04-30  3.240983e-02 -9.586579e-02  1.282756e-01
> 1931-05-31  1.265823e-02 -1.371769e-01  1.498352e-01
> 1931-06-30 -4.050000e-02  1.390169e-01 -1.795169e-01
> 1931-07-31  1.250651e-02 -7.417397e-02  8.668049e-02
> 1931-08-31  7.411220e-02  9.468318e-03  6.464388e-02
> 1931-09-30  2.395783e-02 -2.994228e-01  3.233806e-01
> 1931-10-31  1.450632e-02  8.444902e-02 -6.994270e-02
> 1931-11-30  1.199262e-01 -9.781576e-02  2.177420e-01
> 1931-12-31  2.883031e-03 -1.452632e-01  1.481462e-01
> 1932-01-31  5.708419e-02 -2.832512e-02  8.540931e-02
> 1932-02-29 -5.827506e-03  5.069708e-02 -5.652459e-02
> 1932-03-31 -2.344666e-03 -1.182147e-01  1.158701e-01
> 1932-04-30  1.605954e-02 -2.024624e-01  2.185219e-01
> 1932-05-31 -4.279106e-02 -2.332762e-01  1.904851e-01
> 1932-06-30  1.123641e-01 -8.948546e-03  1.213126e-01
> 1932-07-31  4.561912e-02  3.769752e-01 -3.313561e-01
> 1932-08-31  9.799169e-02  3.754098e-01 -2.774181e-01
> 1932-09-30 -4.888048e-02 -3.694875e-02 -1.193173e-02
> 1932-10-31 -1.992706e-01 -1.386139e-01 -6.065670e-02
> 1932-11-30 -1.337474e-01 -5.890805e-02 -7.483937e-02
> 1932-12-31  2.533461e-02  5.190840e-02 -2.657379e-02
> 1933-01-31  6.247086e-02  7.256894e-03  5.521397e-02
> 1933-02-28  2.150066e-02 -1.844380e-01  2.059387e-01
> 1933-03-31  7.989691e-02  3.356890e-02  4.632800e-02
> 1933-04-30 -9.546539e-03  4.222222e-01 -4.317688e-01
> :
> :
> :
> 2001-09-30  6.406681e-02 -8.074556e-02  1.448124e-01
> 2001-10-31  8.126906e-02  1.907934e-02  6.218972e-02
> 2001-11-30  6.058320e-02  7.668909e-02 -1.610589e-02
> 2001-12-31  5.758811e-02  8.766631e-03  4.882148e-02
> 2002-01-31  4.180194e-02 -1.458492e-02  5.638686e-02
> 2002-02-28 -3.106290e-02 -1.928891e-02 -1.177399e-02
> 2002-03-31  3.999836e-02  3.761344e-02  2.384916e-03
> 2002-04-30  3.872519e-02 -6.060114e-02  9.932633e-02
> 2002-05-31 -2.358079e-02 -7.341115e-03 -1.623967e-02
> 2002-06-30  5.545330e-02 -7.120423e-02  1.266575e-01
> 2002-07-31 -3.119457e-02 -7.792881e-02  4.673424e-02
> 2002-08-31 -4.948639e-03  6.546929e-03 -1.149557e-02
> 2002-09-30 -2.738231e-02 -1.085825e-01  8.120014e-02
> 2002-10-31  6.325486e-02  8.793250e-02 -2.467764e-02
> 2002-11-30  2.032765e-02  5.880188e-02 -3.847423e-02
> 2002-12-31  5.885990e-02 -5.871740e-02  1.175773e-01
> 2003-01-31 -5.023653e-02 -2.614587e-02 -2.409065e-02
> 2003-02-28 -1.891104e-02 -1.502581e-02 -3.885236e-03
> 2003-03-31  9.776745e-02  9.682245e-03  8.808521e-02
> 2003-04-30 -3.007423e-02  8.233138e-02 -1.124056e-01
> 2003-05-31 -2.052851e-02  5.263665e-02 -7.316516e-02
> 2003-06-30  2.465663e-02  1.277463e-02  1.188200e-02
> 2003-07-31 -1.562116e-02  1.763946e-02 -3.326062e-02
> 2003-08-31  6.207876e-02  1.946365e-02  4.261511e-02
> 2003-09-30 -5.278210e-02 -1.058773e-02 -4.219437e-02
> 2003-10-31 -4.240781e-03  5.654044e-02 -6.078122e-02
> 2003-11-30 -7.878209e-02  8.787858e-03 -8.756995e-02
> 2003-12-31  4.905010e-03  5.240762e-02 -4.750261e-02
> 2004-01-31  3.545729e-02  1.835153e-02  1.710576e-02
> 2004-02-29 -9.112422e-02  1.389535e-02 -1.050196e-01
> 2004-03-31 -6.331616e-02 -1.508434e-02 -4.823182e-02
> 2004-04-30  7.764807e-02 -1.567786e-02  9.332593e-02
> 2004-05-31  6.710015e-03  1.369499e-02 -6.984975e-03
> 2004-06-30 -2.433036e-02  1.943932e-02 -4.376967e-02
> 2004-07-31 -9.849356e-03 -3.314085e-02  2.329149e-02
> 2004-08-31 -6.253705e-02  4.016675e-03 -6.655372e-02
> 2004-09-30 -8.074556e-02  1.082333e-02 -9.156889e-02
> 2004-10-31  1.907934e-02  1.527709e-02  3.802254e-03
> 2004-11-30  7.668909e-02  4.044818e-02  3.624091e-02
> 2004-12-31  8.766631e-03  3.401843e-02 -2.525180e-02
> 2005-01-31 -1.458492e-02 -2.437486e-02  9.789944e-03
> 2005-02-28 -1.928891e-02  2.103283e-02 -4.032173e-02
> 2005-03-31  3.761344e-02 -1.768947e-02  5.530291e-02
> 2005-04-30 -6.060114e-02 -1.895654e-02 -4.164460e-02
> 2005-05-31 -7.341115e-03  3.179020e-02 -3.913132e-02
> 2005-06-30 -7.120423e-02  1.433108e-03 -7.263733e-02
> 2005-07-31 -7.792881e-02  3.717322e-02 -1.151020e-01
> 2005-08-31  6.546929e-03 -9.129511e-03  1.567644e-02
> 2005-09-30 -1.085825e-01  8.095481e-03 -1.166779e-01
> 2005-10-31  8.793250e-02 -1.667949e-02  1.046120e-01
> 2005-11-30  5.880188e-02  3.778370e-02  2.101818e-02
> 2005-12-31 -5.871740e-02  3.544358e-04 -5.907184e-02
> 2006-01-31 -2.614587e-02  2.647700e-02 -5.262287e-02
> 2006-02-28 -1.502581e-02  2.705295e-03 -1.773110e-02
> 2006-03-31  9.682245e-03  1.244575e-02 -2.763504e-03
> 2006-04-30  8.233138e-02  1.341893e-02  6.891245e-02
> 2006-05-31  5.263665e-02 -2.874538e-02  8.138203e-02
> 2006-06-30  1.277463e-02  1.327710e-03  1.144692e-02
> 2006-07-31  1.763946e-02  6.163661e-03  1.147580e-02
> 2006-08-31  1.946365e-02  2.375707e-02 -4.293421e-03
> 2006-09-30 -1.058773e-02  2.575373e-02 -3.634146e-02
> 2006-10-31  5.654044e-02  3.256648e-02  2.397396e-02
> 2006-11-30  8.787858e-03  1.898470e-02 -1.019684e-02
> 2006-12-31  5.240762e-02  1.402475e-02  3.838287e-02
> 2007-01-31  1.835153e-02  1.510802e-02  3.243511e-03
> 2007-02-28  1.389535e-02 -1.950030e-02  3.339565e-02
> 2007-03-31 -1.508434e-02  1.115994e-02 -2.624428e-02
> 2007-04-30 -1.567786e-02  4.428216e-02 -5.996002e-02
> 2007-05-31  1.369499e-02  3.485995e-02 -2.116496e-02
> 2007-06-30  1.943932e-02 -1.660254e-02  3.604186e-02
> 2007-07-31 -3.314085e-02 -3.096746e-02 -2.173394e-03
> 2007-08-31  4.016675e-03  1.496826e-02 -1.095158e-02
> 2007-09-30  1.082333e-02  3.735960e-02 -2.653627e-02
> 2007-10-31  1.527709e-02  1.590409e-02 -6.270021e-04
> 2007-11-30  4.044818e-02 -4.182397e-02  8.227215e-02
> 2007-12-31  3.401843e-02 -6.920090e-03  4.093852e-02
> 2008-01-31 -2.437486e-02 -5.997385e-02  3.559899e-02
> 2008-02-29  2.103283e-02 -3.245979e-02  5.349262e-02
> 2008-03-31 -1.768947e-02 -4.327525e-03 -1.336194e-02
> 2008-04-30 -1.895654e-02  4.869178e-02 -6.764832e-02
> 2008-05-31  3.179020e-02  1.295084e-02  1.883936e-02
> 2008-06-30  1.433108e-03 -8.422389e-02  8.565700e-02
> 2008-07-31  3.717322e-02 -8.409418e-03  4.558264e-02
> 2008-08-31 -9.129511e-03  1.447345e-02 -2.360296e-02
> 2008-09-30  8.095481e-03 -8.900728e-02  9.710276e-02
> 2008-10-31 -1.667949e-02 -1.679282e-01  1.512487e-01
> 2008-11-30  3.778370e-02 -7.177816e-02  1.095619e-01
> 2008-12-31  3.544358e-04  1.053659e-02 -1.018215e-02
> 2009-01-31  2.647700e-02 -8.418612e-02  1.106631e-01
> 2009-02-28  2.705295e-03 -1.061347e-01  1.088400e-01
> 2009-03-31  1.244575e-02  8.743881e-02 -7.499306e-02
> 2009-04-30  1.341893e-02  9.562540e-02 -8.220646e-02
> 2009-05-31 -2.874538e-02  5.586690e-02 -8.461228e-02
> 2009-06-30  1.327710e-03  1.997108e-03 -6.693984e-04
> 2009-07-31  6.163661e-03  7.553988e-02 -6.937622e-02
> 2009-08-31  2.375707e-02  3.605340e-02 -1.229633e-02
> 2009-09-30  2.575373e-02  3.728858e-02 -1.153485e-02
> 2009-10-31  3.256648e-02 -1.855341e-02  5.111988e-02
> 2009-11-30  1.898470e-02  5.996658e-02 -4.098189e-02
> 2009-12-31  1.402475e-02  1.930693e-02 -5.282174e-03
> 2010-01-31  1.510802e-02 -3.591852e-02  5.102654e-02
> 2010-02-28 -1.950030e-02  3.093380e-02 -5.043410e-02
> 2010-03-31  1.115994e-02  6.032170e-02 -4.916176e-02
> 2010-04-30  4.428216e-02  1.579164e-02  2.849052e-02
> 2010-05-31  3.485995e-02 -7.976831e-02  1.146283e-01
> 2010-06-30 -1.660254e-02 -5.227779e-02  3.567525e-02
> 2010-07-31 -3.096746e-02  7.003093e-02 -1.009984e-01
>> sum(na.omit(Y[,3]))
> [1] 6.994696
>> 
> 
> 
> It is not a problem with merge as illustrated below...
>> A.x[,4]
>                    Ret
> 1928-01-31            NA
> 1928-02-29 -1.764371e-02
> 1928-03-31  1.083430e-01
> 1928-04-30  3.240983e-02
> 1928-05-31  1.265823e-02
> 1928-06-30 -4.050000e-02
> 1928-07-31  1.250651e-02
> 1928-08-31  7.411220e-02
> 1928-09-30  2.395783e-02
> 1928-10-31  1.450632e-02
> 1928-11-30  1.199262e-01
> 1928-12-31  2.883031e-03
> 1929-01-31  5.708419e-02
> 1929-02-28 -5.827506e-03
> 1929-03-31 -2.344666e-03
> 1929-04-30  1.605954e-02
> 1929-05-31 -4.279106e-02
> :
> :
> :
> 1970-02-28  5.575161e-02
> 1970-03-31  4.357730e-03
> 1970-04-30 -8.747074e-02
> 1970-05-31 -5.777728e-02
> 1970-06-30 -4.663631e-02
> 1970-07-31  7.687028e-02
> 1970-08-31  4.778998e-02
> 1970-09-30  3.618744e-02
> 1970-10-31 -8.312546e-03
> 1970-11-30  5.057046e-02
> 1970-12-31  5.974776e-02
> 1971-01-31  4.319049e-02
> 1971-02-28  1.168123e-02
> 1971-03-31  3.937983e-02
> 1971-04-30  3.887935e-02
> 1971-05-31 -3.905711e-02
> 1971-06-30  3.312228e-03
> 1971-07-31 -3.871620e-02
> 1971-08-31  3.880526e-02
> 1971-09-30 -4.433059e-03
> 1971-10-31 -3.914985e-02
> 1971-11-30  2.120870e-04
> 1971-12-31  8.883940e-02
> 1972-01-31  2.057011e-02
> 1972-02-29  2.770806e-02
> 1972-03-31  8.257537e-03
> 1972-04-30  6.809705e-03
> :
> :
> :
> 2009-03-31  8.743881e-02
> 2009-04-30  9.562540e-02
> 2009-05-31  5.586690e-02
> 2009-06-30  1.997108e-03
> 2009-07-31  7.553988e-02
> 2009-08-31  3.605340e-02
> 2009-09-30  3.728858e-02
> 2009-10-31 -1.855341e-02
> 2009-11-30  5.996658e-02
> 2009-12-31  1.930693e-02
> 2010-01-31 -3.591852e-02
> 2010-02-28  3.093380e-02
> 2010-03-31  6.032170e-02
> 2010-04-30  1.579164e-02
> 2010-05-31 -7.976831e-02
> 2010-06-30 -5.227779e-02
> 2010-07-31  7.003093e-02
> 
>> A.x["1929::2010",4]
>                    Ret
> 1929-01-31  1.720000e+00
> 1929-02-28  1.130000e+00
> 1929-03-31  6.700000e-01
> 1929-04-30  1.900000e-01
> 1929-05-31  3.000000e-02
> 1929-06-30  1.300000e-01
> 1929-07-31  3.000000e-01
> 1929-08-31  2.100000e-01
> 1929-09-30  1.600000e-01
> 1929-10-31  1.800000e-01
> 1929-11-30  1.800000e-01
> 1929-12-31  1.800000e-01
> 1930-01-31  1.700000e-01
> 1930-02-28  1.200000e-01
> 1930-03-31  7.000000e-02
> 1930-04-30  5.000000e-02
> 1930-05-31  5.000000e-02
> 1930-06-30  6.000000e-02
> 1930-07-31  1.100000e-01
> :
> :
> :
> 2003-03-31  9.776745e-02
> 2003-04-30 -3.007423e-02
> 2003-05-31 -2.052851e-02
> 2003-06-30  2.465663e-02
> 2003-07-31 -1.562116e-02
> 2003-08-31  6.207876e-02
> 2003-09-30 -5.278210e-02
> 2003-10-31 -4.240781e-03
> 2003-11-30 -7.878209e-02
> 2003-12-31  4.905010e-03
> 2004-01-31  3.545729e-02
> 2004-02-29 -9.112422e-02
> 2004-03-31 -6.331616e-02
> 2004-04-30  7.764807e-02
> 2004-05-31  6.710015e-03
> 2004-06-30 -2.433036e-02
> 2004-07-31 -9.849356e-03
> 2004-08-31 -6.253705e-02
> 2004-09-30 -8.074556e-02
> 2004-10-31  1.907934e-02
> 2004-11-30  7.668909e-02
> 2004-12-31  8.766631e-03
> 2005-01-31 -1.458492e-02
> 2005-02-28 -1.928891e-02
> 2005-03-31  3.761344e-02
> 2005-04-30 -6.060114e-02
> 2005-05-31 -7.341115e-03
> 2005-06-30 -7.120423e-02
> 2005-07-31 -7.792881e-02
> 2005-08-31  6.546929e-03
> 2005-09-30 -1.085825e-01
> 2005-10-31  8.793250e-02
> 2005-11-30  5.880188e-02
> 2005-12-31 -5.871740e-02
> 2006-01-31 -2.614587e-02
> 2006-02-28 -1.502581e-02
> 2006-03-31  9.682245e-03
> 2006-04-30  8.233138e-02
> 2006-05-31  5.263665e-02
> 2006-06-30  1.277463e-02
> 2006-07-31  1.763946e-02
> 2006-08-31  1.946365e-02
> 2006-09-30 -1.058773e-02
> 2006-10-31  5.654044e-02
> 2006-11-30  8.787858e-03
> 2006-12-31  5.240762e-02
> 2007-01-31  1.835153e-02
> 2007-02-28  1.389535e-02
> 2007-03-31 -1.508434e-02
> 2007-04-30 -1.567786e-02
> 2007-05-31  1.369499e-02
> 2007-06-30  1.943932e-02
> 2007-07-31 -3.314085e-02
> 2007-08-31  4.016675e-03
> 2007-09-30  1.082333e-02
> 2007-10-31  1.527709e-02
> 2007-11-30  4.044818e-02
> 2007-12-31  3.401843e-02
> 2008-01-31 -2.437486e-02
> 2008-02-29  2.103283e-02
> 2008-03-31 -1.768947e-02
> 2008-04-30 -1.895654e-02
> 2008-05-31  3.179020e-02
> 2008-06-30  1.433108e-03
> 2008-07-31  3.717322e-02
> 2008-08-31 -9.129511e-03
> 2008-09-30  8.095481e-03
> 2008-10-31 -1.667949e-02
> 2008-11-30  3.778370e-02
> 2008-12-31  3.544358e-04
> 2009-01-31  2.647700e-02
> 2009-02-28  2.705295e-03
> 2009-03-31  1.244575e-02
> 2009-04-30  1.341893e-02
> 2009-05-31 -2.874538e-02
> 2009-06-30  1.327710e-03
> 2009-07-31  6.163661e-03
> 2009-08-31  2.375707e-02
> 2009-09-30  2.575373e-02
> 2009-10-31  3.256648e-02
> 2009-11-30  1.898470e-02
> 2009-12-31  1.402475e-02
> 2010-01-31  1.510802e-02
> 2010-02-28 -1.950030e-02
> 2010-03-31  1.115994e-02
> 2010-04-30  4.428216e-02
> 2010-05-31  3.485995e-02
> 2010-06-30 -1.660254e-02
> 2010-07-31 -3.096746e-02
> 
> -- 
> Dig it out of the ground
> Melt it down
> Bury it
> Guard it
> 
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list