Quantcast
Channel: Active questions tagged xts - Stack Overflow
Viewing all articles
Browse latest Browse all 57

Matching R time series class between input and output

$
0
0

I have an R function that analyzes regular time series, originally written by assuming the data were just a vector, say of length N. It outputs a list of vectors that are either of length N or length N+1 adding a t=0 result. So here is a conceptual example:

tsf <- function(ts){  extend <- c(0.5,ts)     # A time step before the first in ts  twotimes <- 2.*ts  return (list(extend=extend,twotimes=twotimes))}

the function is intended for univariate geophysical series data that have a (possibly gappy) regular time index based on datetimes and a time step that is some number of minutes. I'm more fluent in Pandas/Python, so to me the playing field for time series classes in R seems wide, possibly including zoo, xts as well as just the time series class or even a data frame with POSIXct column. In xts:

My question what is the simplest (small code, low dependency) way to address the matching problem so that extend and twotimes are of the same class as ts. For instance given this data:

tseq <- seq(from = as.POSIXct('2005-01-01 00:00',tz=''), length.out = 5, by = "15 min")x <- rnorm(5)xdata <- xts(data=x,order.by=tseq)zdata <- zooreg(data=x,order.by=tseq)

Could the following (which are not really functional):

tsf(x)tsf(xdata)tsf(zdata)

be made to both produce like output as their argument? Is the R way to create a tsf.zoo, tsf.xts etc and have these focus on incoming and outgoing coersion and the indexing? Will these type-specific functions cause issues if the user elects not to install one of the libraries like xts?


Viewing all articles
Browse latest Browse all 57

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>