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

Period apply after filtering missing data

$
0
0

I have a large xts of 10 minute data that I am trying to average hourly. The data also has some missing data within it. I want to average all of the hours that have >50% data completeness but replace all hours that have <50% data completeness with NA. How would I go about doing that?

Representative data:

library(lubridate)library(xts)set.seed(1001)starttime <- ydm_hms('2001-01-01 10:00:00')endtime <- ydm_hms('2001-01-01 12:50:00')timevec <- seq(starttime,endtime,by='10 min')data <- data.frame(replicate(2,sample(0:10,length(timevec),rep=TRUE)))datana <- as.matrix(data)datana[sample(seq_along(datana),0.4*length(datana))] <- NAdat_na <- as.data.frame(datana)|> type.convert(as.is=TRUE)dat_natime <- cbind(time=timevec,dat_na)xtsdatna <- xts(dat_natime,order.by = dat_natime$time)

Expected result

                 time   X1   X21 2001-01-01 10:00:00 4.50 6.202 2001-01-01 11:00:00   NA 8.003 2001-01-01 12:00:00 6.25 4.67

Viewing all articles
Browse latest Browse all 57

Trending Articles



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