I have data in this format
> data = data.table(id = 1:10, date = seq(as.Date("2016-01-01"), by = 1, length = 10))
> data
id date
1: 1 2016-01-01
2: 2 2016-01-02
3: 3 2016-01-03
4: 4 2016-01-04
5: 5 2016-01-05
6: 6 2016-01-06
7: 7 2016-01-07
8: 8 2016-01-08
9: 9 2016-01-09
10: 10 2016-01-10
I have another matrix which is the queries / subsets that I wish to preform.
> query = data.table(id = c(1,4,7), date_start = c("2016-01-01", "2016-01-01", "2016-01-01"), date_end = c("2016-01-04", "2016-01-02", "2016-01-03"))
> query
id date_start date_end
1: 1 2016-01-01 2016-01-04
2: 4 2016-01-01 2016-01-02
3: 7 2016-01-01 2016-01-03
I wish to do something like this:
subset(data, (id == query[1] & date > date_start[1] & date < date_end[1]) |
(id == query[2] & date > date_start[2] & date < date_end[2]) |
(id == query[3] & date > date_start[3] & date < date_end[3]))
Is there a automatically generate the subset query without using a for-loop and rbinding the result.
Thanks