I recognize there are several related questions, but I seem to be stumbling somewhere here. I followed this thread as best I could: Interpolating timeseries, but get error messages (see below) :
My dataset contains samples collected every four hours everyday. I would like to interpolate these data into hourly values. Below is a subsample of my much larger dataset:
vis <- structure(list(datetime = structure(1:24, .Label = c("2002-05-01-00",
"2002-05-01-06", "2002-05-01-12", "2002-05-01-18", "2002-05-02-00",
"2002-05-02-06", "2002-05-02-12", "2002-05-02-18", "2002-05-03-00",
"2002-05-03-06", "2002-05-03-12", "2002-05-03-18", "2002-05-04-00",
"2002-05-04-06", "2002-05-04-12", "2002-05-04-18", "2002-05-05-00",
"2002-05-05-06", "2002-05-05-12", "2002-05-05-18", "2002-05-06-00",
"2002-05-06-06", "2002-05-06-12", "2002-05-06-18"), class = "factor"),
VIStot = c(0L, 128L, 359L, 160L, 1L, 121L, 316L, 162L, 1L,
132L, 339L, 163L, 2L, 137L, 364L, 155L, 3L, 122L, 345L, 179L,
3L, 125L, 147L, 77L)), .Names = c("datetime", "VIStot"), class = "data.frame", row.names = c(NA,
-24L))
My code to interpolate to hourly resolution is as follows:
vis[, c(2)] <- sapply(vis[, c(2)], as.numeric)
library(zoo)
vis$datetime <- as.POSIXct(vis$datetime, format="%Y-%m-%d-%H")
hr <- zoo(vis$VIStot, vis$datetime)
int <- na.spline(hr$VIStot)
This ends with the error message
Error in $.zoo(hr, VIStot) : not possible for univariate zoo series
Am I not formatting the datetime correctly? Why is hr
not reading both VIStot
and datetime
?
Also, once interpolated, I would like to export the values in a .csv file format.