I have one data frame which has columns date and spread values (b
). Now I want to match this with a vector of dates (a
) where, if the date is in my data frame, keep the value, if not, leave it as NA. As an example, b
is my data frame and a
is the vector of dates. If the date in a
is also in b
, I want to put the spread value in front of that date, and if not, leave it NA
(2005-01-08 and 2005-01-09 should be NA since it's not in b
). How can I match these dates without doing a for loop?
Note that this is just a small example and I have a large data frame which would take me forever to do a for loop to match the dates.
a <- seq.Date(from = as.Date("2005-01-03"), to = as.Date("2005-01-14"), by = "days")
dput(b)
structure(list(date = structure(c(12786, 12787, 12788, 12789,
12790, 12793, 12794, 12795, 12796, 12797), class = "Date"), `1yspread` = c(0.00126138101604278,
0.00172, 0.0014115, 0.0014115, 0.0014595, 0.0021, 0.00204285714285714,
0.00205714285714286, 0.00205714285714286, 0.00208257142857143
)), row.names = c(NA, 10L), class = "data.frame")