I think no need to use an xts object here since the index is the same for all observations and the right id here is the bid variable. So I explain my solution using 1 simple data.frame as shown above:
DT ## the day before
day bid size
1 1 508.10 10
2 1 509.12 8
3 1 509.10 10
DT1 ## the current or last day
day bid size
1 2 508.1 -10
2 2 514.0 10
Now using merge
we have nearly the solution:
dtm
bid day.x size.x day.y size.y
1 508.10 1 10 2 -10
2 509.10 1 10 NA NA
3 509.12 1 8 NA NA
4 514.00 NA NA 2 10
Now we should just adjust size and remove bids with a null positions. I cerate here an intermediate %+% function to deal with missing values.
## compute size
"%+%" <- function(x,y)
ifelse(is.na(x),
ifelse(is.na(y),NA,y),
ifelse(is.na(y),x,NA))
## remove numm poistion(size==0)
subset(transform(dtm,size=size.x%+%size.y,day=max(day.y,na.rm=T)),
size !=0,select=c(day,bid,size))
day bid size
2 2 509.10 10
3 2 509.12 8
4 2 514.00 10
I think you can get more succicent and syntax sugar solution here using data.table
.