Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting XTS periodicity on non OHLC

Tags:

dataframe

r

xts

I am working with xts or data.frame objects and require a simple means to roll up data that is of 1 minute intervals into 15 minute, hourly, etc...

I realize that there are the to.period methods but my issue is that my columns are non OHLC columns so they are getting dropped when calling to.period.

My data has three columns: POSIXct, SomeVar, AnotherVar.

I need the ability to convert up this data and while doing so either sum up my data columns or take then max. Very similar to how to.period works, but with columns that are named differently. Also, my column data is sometimes factor rather than numeric so it would be ideal if this could handle the conversion as well (when calculating).

like image 267
Dave Avatar asked Jan 19 '23 13:01

Dave


1 Answers

Use period.apply (or apply.daily, apply.weekly, etc.) with your own custom function. Something like:

library(quantmod)
getSymbols("SPY")
myFun <- function(x) c(Low=min(Lo(x)),Vol=sum(Vo(x)))
out <- period.apply(SPY, endpoints(SPY,"years"), myFun)
#               Low         Vol
# 2007-12-31 136.75 39313707500
# 2008-12-31  74.34 75960174800
# 2009-12-31  67.10 62061939800
# 2010-12-31 101.13 52842325500
# 2011-10-10 107.43 42314587300
like image 75
Joshua Ulrich Avatar answered Jan 21 '23 12:01

Joshua Ulrich