Search code examples
rtime-seriesxts

How to calculate daily standard deviation from data collected hourly in R?


I calculated daily means from hourly data for all four variables in the dataset without any issues using the xts function daily.apply(df.xts, FUN=mean). By doing that I get daily averages of each of my variables. However, I am not being able to do the same for the standard deviation. When using daily.apply(df.xts, FUN=sd) I just get the index (time stamps) and only one column with values as an output. What am I missing? Similar issue for var. Thank you


Solution

  • See issue's 124 and 128 on github to see the reasoning why.

    Solution (for now?), use an extra package called matrixStats. The code below will return the standard deviation per column of an xts object.

    apply.daily(df.xts, matrixStats::colSds)