This is particularly complicated and a bit of an edge case, so I wouldn't try too hard to understand what's going on, but I'll try to explain anyway.
Your code creates two calls to the subset-replacement function (xts:::`[<-.xts`). The first call replaces the value of "Close" in a temporary copy of the first row of the object on the left-hand-side of the assignment. This works fine. The second call tries to replace the first *element* of the object on the left-hand-side of the assignment with the modified temporary copy of the first row. This is the problem. This throws a warning because x[1] <- 1:4 is different from x[1,] <- 1:4. The former replaces the first element, the latter replaces the first row. If you want your original command to work, you need to use the comma in your first subset call on the left-hand-side. sample.xts["2007-01-02::2007-01-04",]$Close <- sample.xts["2007-01-02::2007-01-04"]$Close+1 That said, I wouldn't encourage you to do this, because the code isn't clear and it's likely you'll forget the comma and re-confuse yourself at a later date. Simply remember to subset by column first, then row, if you insist on making two calls to the subset-replacement function. I would encourage you to use Arun's solution. It's cleaner and faster because it's only one function call and it avoids the "$" function (which is marginally slower on xts objects). Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com On Thu, Jan 16, 2014 at 7:53 PM, ce <zadi...@excite.com> wrote: > Indeed it works ! Thanks a lot. But why? > > > -----Original Message----- > From: "arun" [smartpink...@yahoo.com] > Date: 01/16/2014 08:44 PM > To: "r-help@r-project.org" <r-help@r-project.org> > Subject: Re: [R] xts error: number of items to replace is not a multiple of > replacement length > > Hi, > Try: > sample.xts["2007-01-02::2007-01-04","Close"] > <-sample.xts["2007-01-02::2007-01-04","Close"] +1 > > > sample.xts["2007-01-02::2007-01-04"] > # Open High Low Close > #2007-01-02 50.03978 50.11778 49.95041 52.11778 > #2007-01-03 50.23050 50.42188 50.23050 52.39767 > #2007-01-04 50.42096 50.42096 50.26414 52.33236 > A.K. > > > On Thursday, January 16, 2014 8:34 PM, ce <zadi...@excite.com> wrote: > > Dear all , > > I am getting this error while trying to change columns of an xts object with > a date range as index. > >> library(xts) > Loading required package: zoo > > Attaching package: ‘zoo’ > > The following object is masked from ‘package:base’: > > as.Date, as.Date.numeric > >> data(sample_matrix) >> sample.xts <- as.xts(sample_matrix, descr='my new xts object') >> head(sample.xts) > Open High Low Close > 2007-01-02 50.03978 50.11778 49.95041 50.11778 > 2007-01-03 50.23050 50.42188 50.23050 50.39767 > 2007-01-04 50.42096 50.42096 50.26414 50.33236 > 2007-01-05 50.37347 50.37347 50.22103 50.33459 > 2007-01-06 50.24433 50.24433 50.11121 50.18112 > 2007-01-07 50.13211 50.21561 49.99185 49.99185 >> >> sample.xts$Close <- sample.xts$Close+1 >> >> head(sample.xts) > Open High Low Close > 2007-01-02 50.03978 50.11778 49.95041 51.11778 > 2007-01-03 50.23050 50.42188 50.23050 51.39767 > 2007-01-04 50.42096 50.42096 50.26414 51.33236 > 2007-01-05 50.37347 50.37347 50.22103 51.33459 > 2007-01-06 50.24433 50.24433 50.11121 51.18112 > 2007-01-07 50.13211 50.21561 49.99185 50.99185 >> >> sample.xts["2007-01-02::2007-01-04"] > Open High Low Close > 2007-01-02 50.03978 50.11778 49.95041 51.11778 > 2007-01-03 50.23050 50.42188 50.23050 51.39767 > 2007-01-04 50.42096 50.42096 50.26414 51.33236 >> >> sample.xts["2007-01-02::2007-01-04"]$Close <- >> sample.xts["2007-01-02::2007-01-04"]$Close+1 > Warning message: > In NextMethod(.Generic) : > number of items to replace is not a multiple of replacement length > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.