1

I want to do a vector assignment of specific dates that happen to be in the reverse order of the xts's index like so:

# create xts index and an all-zeroes instance of xts
idx <- seq.Date( as.Date("2011-01-01"), as.Date("2011-10-01"), by='month' )
x <- xts( seq_along(idx), idx ) 

# create a dataframe with "yearmo" and some data. note reverse order of yermo from idx
d <- data.frame( yearmo=201110:201101, data=10:1 )

# assign d$data to "proper" location in timeseries 
dateVec <- as.Date( paste0( d$yearmo, "01" ), format="%Y%m%d" )
x[ dateVec ] <- d$data

Based on what I know about vector operations in R, this should work to assign each value of the timeseries the corresponding value in the dataframe, where "2011-01-01"'s value would be 1. Instead, the values are reversed, as below:

           [,1]
2011-01-01   10
2011-02-01    9
2011-03-01    8
2011-04-01    7
2011-05-01    6
2011-06-01    5
2011-07-01    4
2011-08-01    3
2011-09-01    2
2011-10-01    1

Whereas I'm expecting

           [,1]
2011-01-01    1
2011-02-01    2
2011-03-01    3
2011-04-01    4
2011-05-01    5
2011-06-01    6
2011-07-01    7
2011-08-01    8
2011-09-01    9
2011-10-01   10

I have checked the xts documentation and some of the similar questions, but I can't find anyone who's run in to a similar thing. What is the proper way to assign multiple xts values at once? What am I missing?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Matthew Turner
  • 3,564
  • 2
  • 20
  • 21
  • Yes, it looks like `xts` object stays in order by time even if you give indexes out of order. Given that behavior, how about `x[dateVec] <- d$data[order(dateVec)]`? – aosmith Jul 10 '14 at 21:58
  • @aosmith Maybe that's the best way to go, but why should it have to be this way? If you can assign with single indexes, why not with vectors? I mean, `x[as.Date("2011-05-01")] <- 42` would assign 42 where we expect. Why not with vector operations? – Matthew Turner Jul 10 '14 at 22:29

1 Answers1

1

Works fine for me if I fix your typos and errors.

idx <- seq.Date(as.Date("2011-01-01"), as.Date("2011-10-01"), by="month")
x <- xts(seq_along(idx), idx) # t() is a function

# create a dataframe with "yearmo" and some data.
# note reverse order of yermo from idx
d <- data.frame(yearmo=201110:201101, data=10:1) # df() is a function

# assign d$data to "proper" location in timeseries 
dateVec <- as.Date(paste0(d$yearmo, "01"), format="%Y%m%d") # fix typo
x[dateVec] <- d$data

Edit: Sorry, I didn't notice the output was reversed from what you expect. The problem is because your dates are in descending order. This would work if they were in ascending order.

d <- d[order(d$yearmo),]
dateVec <- as.Date(paste0(d$yearmo, "01"), format="%Y%m%d")
x[dateVec] <- d$data

That said, this may be a bug... but xts:::[<-.xts is a core xts function that touches almost every other xts function, so I'd have to test a patch extensively before deciding whether or not it's worth attempting to fix.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • OK, definitely a typo and yes, I shouldn't be using `t` and `df` for variable names (bad practice for sure, but I don't think it has an impact), but what is your result for x? I ran your code and x is in the reverse order from what seems sensible. That is, period `2011-01-01`'s value is 10, not 1, and `2011-10-01`'s value is 1, not 10; it's still reversed. – Matthew Turner Jul 10 '14 at 21:16