37

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.

1
  • 2
    If you name your dataframe df and your dates t1 and t2, you can get something shorter like: df[df$Date %in% t1:t2, ]. To clarify, t1:t2 works with dates, so you don't need to have inequalities.
    – PatrickT
    Oct 17, 2015 at 16:20

8 Answers 8

24

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
0
14

You can use the subset() function with the & operator:

subset(IBOV_RET, DATE1> XXXX-XX-XX & DATE2 < XXXX-XX-XX)

Updating for a more "tidyverse-oriented" approach:

IBOV_RET %>%
  filter(DATE1 > XXXX-XX-XX, DATE2 < XXXX-XX-XX) #comma same as &
0
4

There is no real other way to extract date ranges. The logic is the same as extracting a range of numeric values as well, you just need to do the explicit Date conversion as you've done. You can make your subsetting shorter as you would with any other subsetting task with subset or with. You can break ranges into intervals with cut (there is a specific cut.Date overload). But base R does not have any way to specify Date literals so you cannot avoid the conversion. I can't imagine what other sort of syntax you may have had in mind.

3

What about:

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

# creating a data range with the start and end date:
dates <- seq(DATE1, DATE2, by="days")

IBOV_RET <- subset(IBOV_RET, DATE %in% dates)
3

I believe lubridate could help here;

daterange <- interval(DATE1, DATE2)
TEST <- IBOV_RET[which(Date %within% daterange),]
1

I sort of love dplyr package
So if you

>library("dplyr")

and then, as you did:

>Date1<-as.Date("2014-04-01")  
>Date2<-as.Date("2014-05-05")

Finally

>test<-filter(IBOV_RET, filter(DATE>Date1 & DATE<Date2))
1
  • Calling filter twice like this doesn't make sense
    – camille
    Sep 14, 2021 at 19:04
1

You can use R's between() function after simply converting the strings to dates:

df %>%
    filter(between(date_column, as.Date("string-date-lower-bound"), as.Date("string-date-upper-bound")))
0
Test = IBOV_RET[IBOV_RET$Date => "2014-04-01" | IBOV_RET$Date <= "1993-05-04"]

Here I am using "or" function | where data should be greater than particular data or data should be less than or equal to this date.

3
  • 4
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – Yunnosch
    Sep 30, 2021 at 20:16
  • 1
    The comment by @Yunnosch is always useful to keep in mind, but especially here where there's already a couple of answers that have been validated by the community. How does your answer differ from the existing answers? In what conditions might it be preferred? Oct 1, 2021 at 8:28
  • I am sorry for late reply.
    – Gaurav
    Oct 3, 2021 at 3:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.