From 6d4eeab42ebdc422e8c6c658e282f6434c0858e2 Mon Sep 17 00:00:00 2001 From: Wolfgang Ludger Hottgenroth Date: Mon, 9 Aug 2021 18:10:31 +0200 Subject: [PATCH] continued --- secondDbSteps.r | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/secondDbSteps.r b/secondDbSteps.r index 161cc16..18f8096 100644 --- a/secondDbSteps.r +++ b/secondDbSteps.r @@ -1,10 +1,45 @@ library(DBI) library(tidyr) -con <- dbConnect(RPostgres::Postgres(), dbname='mainscnt', host='172.16.10.27', user='wn') -res <-dbSendQuery(con, "select time, location, freq from mainsfrequency where valid=1 and time >= '2021-08-05T00:00:00.0Z' and time < '2021-08-06T00:00:00.0Z'") -devices <- dbFetch(res) -dbClearResult(res) -dbDisconnect(con) +con <- dbConnect(RPostgres::Postgres(), + dbname='mainscnt', + host='172.16.10.27', + user='wn') -d2 <- devices %>% pivot_wider(names_from = location, values_from = freq) +START <- "2021-08-02T14:00:00.0Z" +END <- "2021-08-02T15:00:00.0Z" + +res <-dbSendQuery(con, "select time, location, freq from mainsfrequency where valid=1 and time >= $1 and time < $2") +dbBind(res, list(START, END)) +frequencies <- dbFetch(res) +dbClearResult(res) + +freq_wide <- frequencies %>% + pivot_wider(names_from = location, + values_from = freq, + values_fn = mean) + + +THRESHOLD <- 0.5 + +for (colIdx in 2:length(freq_wide)) { + last <- freq_wide[[1, colIdx]] + for (rowIdx in 1:length(freq_wide[[colIdx]])) { + current <- freq_wide[[rowIdx, colIdx]] + if (!is.na(current) && !is.na(last) && (abs(current - last) > THRESHOLD)) { + freq_wide[[rowIdx, colIdx]] = NA + } + last <- current + } +} + + +freq_wide <- freq_wide %>% + rowwise() %>% + mutate(mad = mad(c_across(names(freq_wide)[-1]), + na.rm=TRUE)) + +# print(summary(freq_wide)) +print(sum(freq_wide$mad) / length(freq_wide$mad)) + +dbDisconnect(con)