It seems you are trying to create a left join. For that one normally uses merge
. The two elements of the argument all = c(TRUE, FALSE)
shown in the code below refer to whether we keep unmatched dates in mon
and a
respectively.
library(zoo)
a <- zoo(cbind(col1 = 1:50, col2 = 11:60), as.Date("2011-12-31") + 0:49)
mon <- zoo(cbind(mc = letters[1:3], mc2 = LETTERS[1:3]),
as.Date(c('2012-01-01', '2012-02-01', '2012-03-01')))
merge(mon, a, all = c(TRUE, FALSE))
giving:
mc mc2 col1 col2
2012-01-01 a A 2 12
2012-02-01 b B 33 43
2012-03-01 c C <NA> <NA>
If you only want col1
then:
merge(mon, a, all = c(TRUE, FALSE))$col1
If you don't need the row with the NA then specify FALSE to eliminate unmatched dates from both mon
and a
:
merge(mon, a, all = FALSE)
INDEXING BY TIME
This can also be done by using time indexing like this;
result <- mon
result$col1 <- a$col1[time(mon)] # does an implicit merge
result
giving:
mc mc2 col1
2012-01-01 a A 2
2012-02-01 b B 33
2012-03-01 c C <NA>
If you don't need the NA row then this would be sufficient:
a[time(mon)]
giving:
col1 col2
2012-01-01 2 12
2012-02-01 33 43
MATCH
1) Although the above approaches are recommended over MATCH
if you do want to use MATCH
for some reason then add the nomatch = 0
argument so that it returns 0 instead of NA for non-matches. That will cause the indexing to simply drop that value. The assignment to result$col1
will do an implicit merge
filling in an NA.
result <- mon
result$col1 <- a$col1[MATCH(time(mon), time(a), nomatch = 0)]
result
giving:
mc mc2 col1
2012-01-01 a A 2
2012-02-01 b B 33
2012-03-01 c C <NA>
result$dol1
can be used to get just col1
.
2) Another way to do this is the following which gives the same result. In this case the right hand side has three elements with the third being NA but since the right hand side is now a plain vector it is just copied element by element into result$col1 rather than doing an implicit merge.
result <- mon
result$col1 <- coredata(a$col1)[MATCH(time(mon), time(a))]
result
Other
Note that what is referred to as row.names in the question is the time index, not row names.
merge.xts(mon, a, join = "left")
. Not sure why you get the na's. Maybe @G. Grothendieck will drop by on this question and he might know why you get the NA's. – phivera[MATCH(index(mon),index(a))]$col1
and there was no match for the last date ina
hence the NA – IRTFM