1

I am doing a multi-period analysis and I need to extract values between two dates from an xts object named data.

The start and end of my calculation lies between those dates. These same dates are used as rownames in a data.frame named results.

start_date <- head(rownames(results), n=1)
end_date <- tail(rownames(results), n=1) 

I also identify the column names with this vector:

> col_names
[1] "Stock1" "Stock2" "Stock3" 

I need to extract the observations in the data object in the columns in col_names and between start_date and end_date.

Joshua Ulrich
  • 163,034
  • 29
  • 321
  • 400
Alex Bădoi
  • 730
  • 2
  • 8
  • 23
  • these variables are yielded from a data frame coming from other calculations. the code i have seems to work just fine. They are both character vectors containing the end and start date – Alex Bădoi Oct 17 '15 at 14:18

1 Answers1

1

Assuming that the 'start_date' and 'end_date' are 'character' vectors in the 'Date' format ('%Y-%m-%d'), we paste them together with sep='/', use that as row index, specify 'col_names' as column index and subset the xts object 'data'.

data[paste(start_date, end_date, sep="/"), col_names]

As a reproducible example

library(xts)
data(sample.xts)
start_date <- '2007-01-02'
end_date <- '2007-01-05' 
col_names <-  c('Open', 'High')
sample.xts[paste(start_date, end_date, sep="/"), col_names]
#               Open     High
#2007-01-02 50.03978 50.11778
#2007-01-03 50.23050 50.42188
#2007-01-04 50.42096 50.42096
#2007-01-05 50.37347 50.37347

If we need the rows which are between the 'start_date' and 'end_date'

sample.xts[index(sample.xts) > start_date &
            index(sample.xts) < end_date, col_names]
#               Open     High
#2007-01-03 50.23050 50.42188
#2007-01-04 50.42096 50.42096
akrun
  • 674,427
  • 24
  • 381
  • 486
  • thats perfect, this is very helpful. is there a way to change the period within that that function? i tried using start_date + 5 and end_date + 5 so that it uses a the period 5 days ahead of the start_date and end but it didn't work. – Alex Bădoi Oct 17 '15 at 14:34
  • 1
    @AlexBădoi Try `as.Date(start_date)+5`. For the example dataset, `sample.xts[paste(as.Date(start_date)+5, as.Date(end_date)+5, sep='/'), col_names]` worked – akrun Oct 17 '15 at 14:35
  • as.Date has inbuilt all calendar days right? because here i have trading days and it excludes weekends. so for example +5 would give me just the next 3 values in some cases because 2 of them are weekends. What i need is the next 5 values from my table rather than the next 5 days. is there another way to do this? – Alex Bădoi Oct 17 '15 at 14:48
  • 1
    @AlexBădoi Yes, you are right it includes all calendar days. One option would be to subset the days using `is.weekend`. I guess this [link](http://stackoverflow.com/questions/26441700/how-to-determine-if-date-is-a-weekend-or-not-not-using-lubridate) helps you in doing that. – akrun Oct 17 '15 at 14:50
  • i think your solution is very helpful but it doesn't fully solve my problem. There is no point in asking the exact same question again – Alex Bădoi Oct 17 '15 at 15:48
  • 1
    @AlexBădoi As I mentioned earlier, my solution was based on your earlier description. If you had correctly mentioned the problem earlier, it would have saved both of our time. Now, it is totally different and should be a new question. Otherwise, people update n number of times and the answerer has to answer each time the question gets updated. – akrun Oct 17 '15 at 15:49
  • 1
    got it. i will post a new question about it. – Alex Bădoi Oct 17 '15 at 15:53