2

Both x and y overlap on 2018-08-08. How can I combine the rows where I keep all values of x, then only retain values of y that do not overlap the same index/date value of x?

x <- as.xts(1:10, Sys.Date()+1:10). 
y <- as.xts(11:20, Sys.Date()+10:19). 
z <- rbind(x,y)

2018-07-30    1
2018-07-31    2
2018-08-01    3
2018-08-02    4
2018-08-03    5
2018-08-04    6
2018-08-05    7
2018-08-06    8
2018-08-07    9
2018-08-08   10
2018-08-08   11
2018-08-09   12
2018-08-10   13
2018-08-11   14
2018-08-12   15
2018-08-13   16
2018-08-14   17
2018-08-15   18
2018-08-16   19
2018-08-17   20

Should be missing the 2018-8-8 11 value for y

2018-07-30    1
2018-07-31    2
2018-08-01    3
2018-08-02    4
2018-08-03    5
2018-08-04    6
2018-08-05    7
2018-08-06    8
2018-08-07    9
2018-08-08   10
2018-08-09   12
2018-08-10   13
2018-08-11   14
2018-08-12   15
2018-08-13   16
2018-08-14   17
2018-08-15   18
2018-08-16   19
2018-08-17   20
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Joe
  • 349
  • 1
  • 2
  • 11

1 Answers1

2

Subset the y's to omit those containing an index in x

z <- rbind(x,y[!(index(y) %in% index(x))])

Chris Holbrook
  • 2,531
  • 1
  • 17
  • 30
  • Thanks Chris! That worked well. Really appreciate your time. – Joe Jul 30 '18 at 21:34
  • Any idea how to do.call this to merge more than 2 series? – Joe Jul 30 '18 at 21:46
  • using this to combine more than two xts objects would get tedious (e.g., something like `z <- rbind(x,y[!(index(y) %in% index(x))], m[!(index(m) %in% c(index(x), index(y))])` for a third object m. Ronak's solution (dropping duplicates after combining) seems simple enough. – Chris Holbrook Jul 31 '18 at 02:57
  • yessir, thanks again – Joe Jul 31 '18 at 03:03