I currently have a xts with multiple columns and rows, where some NAs can be present, just like seen bellow:
utc VR1_40 VR2_40 VR3_40 Q1_40 Q2_40 Q3_40
16.05.18 01:49:20 4 4 4 400 400 400
16.05.18 01:50:20 5 5 5 500 500 500
16.05.18 01:54:48 5 5 5 500 500 500
16.05.18 01:57:54 5 5 5 500 500 500
16.05.18 01:59:54 5 5 5 500 500 500
16.05.18 02:00:16 null 2 2 200 200 null
16.05.18 02:03:10 2 null 2 200 200 null
16.05.18 02:07:12 2 2 null 200 null 200
16.05.18 02:09:52 2 2 2 null 200 null
16.05.18 03:09:52 3 3 3 300 300 300
I want to get a 10 minute average, so I apply the following code:
means.xts <- period.apply(ts, endpoints(ts, "mins", k=10), FUN=mean)
The problem is that a single NA will output NA for the whole 10 minute average.
VR1_40 Q1_40 VR2_40 Q2_40 VR3_40 Q3_40
2018-05-16 01:49:20 4 400 4 400 4 400
2018-05-16 01:59:54 5 500 5 500 5 500
2018-05-16 02:09:52 NA NA NA NA NA NA
2018-05-16 03:09:52 3 300 3 300 3 300
Using na.omit like so
means.xts <- period.apply(na.omit(ts), endpoints(na.omit(ts), "mins", k=10), FUN=mean)
Will exclude the whole NA row from the result.
And defining a custom mean function like so
means.xts <- period.apply(na.omit(ts), endpoints(na.omit(ts), "mins", k=10), FUN = function(x){mean(x, na.rm = TRUE)})
Will average all the columns into a single one.
Seems something simple that I'm missing... any help is appreciated.
Thanks!
One solution is to set na.rm as True. Like this:
means.xts <- period.apply(na.omit(ts), endpoints(na.omit(ts), "mins", k=10), FUN = mean,na.rm=T)