Subset a dataframe between 2 dates

RiskTech picture RiskTech · May 13, 2014 · Viewed 66.8k times · Source

I am working with daily returns from a Brazilian Index (IBOV) since 1993, I am trying to figure out the best way to subset for periods between 2 dates.

The data frame (IBOV_RET) is as follows :

head(IBOV_RET)
        DATE    1D_RETURN
1 1993-04-28 -0.008163265
2 1993-04-29 -0.024691358
3 1993-04-30  0.016877637
4 1993-05-03  0.000000000
5 1993-05-04  0.033195021
6 1993-05-05 -0.012048193
...

I set 2 variables DATE1 and DATE2 as dates

DATE1 <- as.Date("2014-04-01")
DATE2 <- as.Date("2014-05-05")

I was able to create a new subset using this code:

TEST <- IBOV_RET[IBOV_RET$DATE >= DATE1 & IBOV_RET$DATE <= DATE2,]

It worked, but I was wondering if there is a better way to subset the data between 2 date, maybe using subset.

Answer

talat picture talat · May 13, 2014

As already pointed out by @MrFlick, you dont get around the basic logic of subsetting. One way to make it easier for you to subset your specific data.frame would be to define a function that takes two inputs like DATE1 and DATE2 in your example and then returns the subset of IBOV_RET according to those subset parameters.

myfunc <- function(x,y){IBOV_RET[IBOV_RET$DATE >= x & IBOV_RET$DATE <= y,]}

DATE1 <- as.Date("1993-04-29")
DATE2 <- as.Date("1993-05-04")

Test <- myfunc(DATE1,DATE2)    

#> Test
#        DATE  X1D_RETURN
#2 1993-04-29 -0.02469136
#3 1993-04-30  0.01687764
#4 1993-05-03  0.00000000
#5 1993-05-04  0.03319502

You can also enter the specific dates directly into myfunc:

myfunc(as.Date("1993-04-29"),as.Date("1993-05-04")) #will produce the same result