0

had this kind of data set

id   value   Date
1    15      "2016-01-01"
1    12      "2016-01-02"
1    13      "2016-01-03"
2    20      "2016-01-03"
2    40      "2016-06-04"
2    60      "2017-02-05"
3    100     "2016-01-15"
3    20      "2016-02-05"
3    50      "2016-04-06"
3    60      "2017-07-02"

I have a list of dates that I want to subset from:

id   Start_Date    End Date
1    "2016-01-01"  "2016-05-01"
2    "2016-03-01"  "2016-07-01"
3    "2016-04-01"  "2016-08-01"

There are a few solutions. One is to use for loop to loop through the list of dates to subset from the main data set. The other is to simply attached the list of dates to the data set, and create an indicator or some sorts (1,0) to indicate the rows you would like to subset.

Is there a way to subset these individualized dates using dplyr? The main aim will be to keep all columns of the data set.

Expected output is:

id   value   Date
1    15      "2016-01-01"
1    12      "2016-01-02"
1    13      "2016-01-03"
2    40      "2016-06-04"
3    50      "2016-04-06"

many thanks in advance!

bzzbzzRzzbzz
  • 111
  • 11
  • 2
    If you have valid date columns there (rather characters), just `library(data.table) ; setDT(df)[lkp, on = .(id, Date >= Start_Date, Date <= End_Date)]` and you all set. – David Arenburg Mar 02 '17 at 07:55
  • 1
    Or if you want to use `dplyr` `left_join(dat1,dat2,by=c("id")) %>% filter(Date >= startDate & Date <= endDate)` – count Mar 02 '17 at 08:08

0 Answers0