69 lines
1.8 KiB
R
69 lines
1.8 KiB
R
library(DBI)
|
|
library(tidyr)
|
|
library(dplyr)
|
|
library(lubridate)
|
|
library(R.utils)
|
|
|
|
|
|
get_freq_df <- function(con, startDate, endDate) {
|
|
startStr <- strftime(startDate, "%Y-%m-%d %H:%M:%S", tz="UTC")
|
|
endStr <- strftime(endDate, "%Y-%m-%d %H:%M:%S", tz="UTC")
|
|
|
|
res <-dbSendQuery(con, "select time, location, freq from mainsfrequency where valid=1 and time >= $1 and time < $2")
|
|
dbBind(res, list(startStr, endStr))
|
|
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
|
|
}
|
|
}
|
|
|
|
return (freq_wide)
|
|
}
|
|
|
|
con <- dbConnect(RPostgres::Postgres(),
|
|
dbname='mainscnt',
|
|
host='172.16.10.27',
|
|
user='wn')
|
|
|
|
|
|
START <- "2021-08-03 00:00:00"
|
|
INTERVAL <- 3600
|
|
|
|
for (offset in 0:0) {
|
|
startDate <- ymd_hms(START) + INTERVAL * offset
|
|
endDate <- startDate + INTERVAL
|
|
|
|
freq_wide <- get_freq_df(con, startDate, endDate)
|
|
|
|
for (colIdx in 2:length(freq_wide)) {
|
|
colName.mean <- paste("mean.w.o.", names(freq_wide)[colIdx], sep="")
|
|
colName.diff <- paste(names(freq_wide)[colIdx], ".to.mean", sep="")
|
|
freq_wide <- freq_wide %>%
|
|
rowwise() %>%
|
|
mutate(!!colName.mean := mean(c_across(names(freq_wide)[c(-1, - colIdx)]), na.rm=TRUE)) %>%
|
|
mutate(!!colName.diff := abs())
|
|
|
|
}
|
|
|
|
# print(summary(freq_wide))
|
|
}
|
|
|
|
dbDisconnect(con)
|
|
|
|
|