with loop but not fine
This commit is contained in:
@ -1,28 +1,40 @@
|
||||
library(DBI)
|
||||
library(tidyr)
|
||||
library(dplyr)
|
||||
library(lubridate)
|
||||
library(R.utils)
|
||||
|
||||
con <- dbConnect(RPostgres::Postgres(),
|
||||
dbname='mainscnt',
|
||||
host='172.16.10.27',
|
||||
host='db.mainscnt.eu',
|
||||
user='wn')
|
||||
|
||||
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)
|
||||
START <- "2021-08-03 00:00:00"
|
||||
INTERVAL <- 3600
|
||||
|
||||
freq_wide <- frequencies %>%
|
||||
for (offset in 0:23) {
|
||||
startDate <- ymd_hms(START) + INTERVAL * offset
|
||||
endDate <- startDate + INTERVAL
|
||||
|
||||
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)
|
||||
|
||||
freq_wide$Linz_AT <- freq_wide$Linz_AT + 0.002
|
||||
|
||||
THRESHOLD <- 0.5
|
||||
THRESHOLD <- 0.5
|
||||
|
||||
for (colIdx in 2:length(freq_wide)) {
|
||||
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]]
|
||||
@ -31,15 +43,16 @@ for (colIdx in 2:length(freq_wide)) {
|
||||
}
|
||||
last <- current
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
freq_wide <- freq_wide %>%
|
||||
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))
|
||||
# print(summary(freq_wide))
|
||||
printf("start: %s, end: %s, mad/cnt: %f\n", startStr, endStr, sum(freq_wide$mad) / length(freq_wide$mad))
|
||||
}
|
||||
|
||||
dbDisconnect(con)
|
||||
|
Reference in New Issue
Block a user