Time-series (xts)

After getting data as a dplyr tibble, the first thing you might want to do is convert it into a time-series (xts) object. Here’s the recommended way:

indices <- Indices() #for IndiaGsecTimeSeries

ycInd <- indices$IndiaGsecTimeSeries() %>%
    filter(NAME == "5_10" & TIME_STAMP >= startDate) %>%
    select(TIME_STAMP, YIELD = YTM) %>%
    collect() %>%
    mutate(TIME_STAMP = as.Date(TIME_STAMP))

ycIndXts <- xts(ycInd$YIELD, ycInd$TIME_STAMP)

Just in case the above code snippet doesn’t render properly:

The mutate has to come after the collect. Otherwise, mutate ends up running as an SQL query in the database and TIME_STAMP remains a string.

Questions? slack me!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s