[Solved] R subset based on range of dates [closed]


Are you asking something like the following?

Let’s say your initial dataframe is df, which is the following:

df
            A          B          C
1  2016-02-16 2016-03-21 2016-01-01
2  2016-07-07 2016-06-17 2016-01-31
3  2016-05-19 2016-09-10 2016-03-01
4  2016-01-14 2016-08-21 2016-04-01
5  2016-09-02 2016-06-15 2016-05-01
6  2016-05-09 2016-07-17 2016-05-31
7  2016-06-13 2016-06-23 2016-07-01
8  2016-09-17 2016-03-11 2016-07-31
9  2016-03-09 2016-05-13 2016-08-30
10 2016-01-20 2016-09-01 2016-09-30

Now if you do the following subset operation, we shall get the following dataframe subset:

df.sub <- df[apply(df, 1, function(x) (x[3] < min(x[1], x[2])) | 
                                      (x[3] > max(x[1], x[2]))),]
df.sub
            A          B          C
1  2016-02-16 2016-03-21 2016-01-01
2  2016-07-07 2016-06-17 2016-01-31
3  2016-05-19 2016-09-10 2016-03-01
5  2016-09-02 2016-06-15 2016-05-01
7  2016-06-13 2016-06-23 2016-07-01
9  2016-03-09 2016-05-13 2016-08-30
10 2016-01-20 2016-09-01 2016-09-30

Hope it helps.

solved R subset based on range of dates [closed]