0

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")

statwoman
  • 380
  • 1
  • 9
  • 1
    You may use `%in%` with `!` and assign (`<-`) to NA. `a[!a %in% b$date] <- NA` – akrun Jan 30 '22 at 20:23
  • I want to add the spread values in ```b``` in another column in ```a``` or another dataframe and leave them ```NA``` if they don't match. @akrun – statwoman Jan 30 '22 at 20:26
  • a is a vector. So you may need `data.frame(a, b = replace(a, !a %in% b$date, NA))` – akrun Jan 30 '22 at 20:27
  • 1
    Or use a join: `result = data.frame(a = a)`, then `merge(result, b, all.x = T, by.x = "a", by.y = "date")` – Gregor Thomas Jan 30 '22 at 20:29
  • I need to keep the values in ```b$`1yspread```, if the dates match and leave it as ```NA``` if they don't. This just keeps the dates from ```b```. @akrun – statwoman Jan 30 '22 at 20:30
  • Thanks @GregorThomas, this is exactly the structure I was looking for! – statwoman Jan 30 '22 at 20:32
  • Glad that worked. Next time I'd suggest posting the desired output for the sample input. This was a little confusing since `a` was not a data frame, and since there were no `NA` values, it wasn't super clear what "leave it as NA" meant. – Gregor Thomas Jan 30 '22 at 20:35

0 Answers0