0

I have a 10x2 data frame called DATA where one column contains prices and the other dates. And I have a vector of dates called KeyDates that containes two key dates.

DATA <- structure(list(Prices = c(1245.7, 1244.4, 1244.8, 1247.2, 1241.4,1241.1, 1239.3, 1240.9, 1239.6, 1234.6), Dates = structure(15156:15165, class = "Date")), .Names = c("Prices","Dates"), row.names = c(NA, 10L), class = "data.frame") 
KeyDates <- structure(15159:15160, class = "Date") 

#DATA Output:
Prices      Dates
1  1245.7 2011-07-01
2  1244.4 2011-07-02
3  1244.8 2011-07-03
4  1247.2 2011-07-04
5  1241.4 2011-07-05
6  1241.1 2011-07-06
7  1239.3 2011-07-07
8  1240.9 2011-07-08
9  1239.6 2011-07-09
10 1234.6 2011-07-10

#KeyDates Output:
[1] 2011-07-04 2011-07-05

I would like to match DATA and KeyDates, and make a new dataframe SUBSET containing the prices and dates for the matched dates.

#desired output for SUBSET:
Prices      Dates
1  1247.2 2011-07-04
2  1241.4 2011-07-05 

How could I achieve that?

This is a simplified example. I would actually be running this in dozens of dates spread across many years. Thank you very much.


EDIT: @David Arenburg the answer to this question is indeed present in R function for testing if a vector contains a given element, yet the question is different. Quite likely this question is a duplicate of some other question, but it is not of the one you marked as duplicate of.

Community
  • 1
  • 1
Krug
  • 1,003
  • 13
  • 33
  • 1
    Try: `DATA[DATA$Dates %in% KeyDates,]` – DatamineR Apr 23 '16 at 20:19
  • Exactly what I needed! Thanks very much. Would you put that as answer so I may mark it as answered? Though maybe question should be deleted, as there are plenty matching questions, none for dates, but plenty still. – Krug Apr 23 '16 at 20:30
  • @Dave2e No. This will only work correctly for one length vector. – David Arenburg Apr 23 '16 at 20:35

0 Answers0