Commit 598ea0d5 authored by Enrico Schumann's avatar Enrico Schumann

[read_ts_tables] New argument 'frequency'

parent 383ac0ca
2019-03-20 Enrico Schumann <es@enricoschumann.net>
* R/functions.R (read_ts_tables): rename
argument 'fread' to 'read.fn', with default
NULL. To use package 'data.table', set it to
"fread".
(read_ts_tables): fixed -- 'drop.weekends' now
works for intraday data
* NAMESPACE: import 'datetimeutils::roundPOSIXt'
* R/functions.R (read_ts_tables): rename
argument 'fread' to 'read.fn', with default
NULL. To use package 'data.table', set it to
"fread".
(read_ts_tables): fixed -- 'drop.weekends'
now works for intraday data
(read_ts_tables): new argument 'frequency',
used only for intraday data
2019-03-11 Enrico Schumann <es@enricoschumann.net>
* R/functions.R (as.ts_table.ts_table): add method
* R/functions.R (as.ts_table.ts_table): add method
2019-02-14 Enrico Schumann <es@enricoschumann.net>
* DESCRIPTION (Imports, Suggests): move package
DBI to Suggests
* DESCRIPTION (Imports, Suggests): move package
DBI to Suggests
2018-11-21 Enrico Schumann <es@enricoschumann.net>
......@@ -43,9 +47,9 @@
2018-02-01 Enrico Schumann <es@enricoschumann.net>
* R/functions.R (read_ts_tables): experimental
new argument 'fread'; if TRUE, files are read
with data.table::fread instead of read.table
* R/functions.R (read_ts_tables): experimental
new argument 'fread'; if TRUE, files are read
with data.table::fread instead of read.table
2017-12-11 Enrico Schumann <es@enricoschumann.net>
......@@ -65,7 +69,7 @@
2017-10-24 Enrico Schumann <es@enricoschumann.net>
* DESCRIPTION (Version): 0.5-0
* DESCRIPTION (Version): 0.5-0
* R/functions.R (ts_table): store timestamp as
numeric (which reverts the change introduced in
......
......@@ -9,7 +9,8 @@ export(
importFrom("datetimeutils",
"is_businessday",
"previous_businessday")
"previous_businessday",
"roundPOSIXt")
importFrom("fastmatch",
"fmatch")
......
......@@ -145,7 +145,7 @@ write_ts_table <- function(ts, dir, file,
sep = ",")
}
} else if (backend == "monetdb") {
if (!inherits(dir, "MonetDBEmbeddedConnection")) {
dir <- DBI::dbConnect(MonetDBLite::MonetDBLite(), dir)
on.exit(DBI::dbDisconnect(dir, shutdown = TRUE))
......@@ -167,12 +167,12 @@ read_ts_tables <- function(file, dir, t.type = "guess",
drop.weekends = TRUE,
column.names = "%dir%/%file%::%column%",
backend = "csv",
read.fn = NULL) {
read.fn = NULL,
frequency = "1 sec") {
backend <- tolower(backend)
if (backend == "csv") {
### ****************
if (missing(dir)) {
dir <- ""
......@@ -181,7 +181,6 @@ read_ts_tables <- function(file, dir, t.type = "guess",
dfile <- file.path(dir, file)
}
if (length(dir) != length(file)) {
if (length(dir) > 1L && length(file) > 1L)
stop("file and dir lengths must match")
......@@ -198,6 +197,7 @@ read_ts_tables <- function(file, dir, t.type = "guess",
samp <- readLines(dfile[[1]], n = 2L)
samp.n <- length(samp)
}
if (missing(start) || t.type == "guess") {
if (samp.n == 2L)
timestamp1 <- as.numeric(
......@@ -205,13 +205,14 @@ read_ts_tables <- function(file, dir, t.type = "guess",
else
timestamp1 <- numeric(0)
}
if (t.type == "guess") {
if (samp.n == 2L) {
t.type <- if (timestamp1 < 43200) ## 86400/2
"Date" else "POSIXct"
} else
t.type <- "Date"
t.type <- if (samp.n == 2L && timestamp1 > 43200) ## 86400/2
"POSIXct"
else
"Date"
}
if (missing(columns)) {
tmp <- gsub("\"", "",
strsplit(samp[[1]], ",", fixed = TRUE)[[1]])
......@@ -235,6 +236,7 @@ read_ts_tables <- function(file, dir, t.type = "guess",
timestamp <- seq(start, end , "1 day")
if (drop.weekends)
timestamp <- timestamp[is_businessday(timestamp)]
timestamp <- c(unclass(timestamp))
} else if (t.type == "POSIXct") {
start <- if (missing(start))
ttime(timestamp1,
......@@ -246,9 +248,14 @@ read_ts_tables <- function(file, dir, t.type = "guess",
end <- as.POSIXct(previous_businessday(Sys.Date()))
else
end <- as.POSIXct(end)
timestamp <- seq(start, end , "1 sec")
if (frequency != "1 sec") {
start <- roundPOSIXt(start, frequency)
end <- roundPOSIXt(end, frequency, up = TRUE)
}
timestamp <- seq(start, end , frequency)
if (drop.weekends)
timestamp <- timestamp[is_businessday(timestamp)]
timestamp <- c(unclass(timestamp))
} else
stop("unknown ", sQuote("t.type"))
......@@ -281,6 +288,7 @@ read_ts_tables <- function(file, dir, t.type = "guess",
if (!is.null(res))
results[ii, (nc*(i-1)+1):(nc*i)] <- as.matrix(res)
}
rm <- rowSums(is.na(results)) == dim(results)[[2L]]
results <- results[!rm, ,drop = FALSE]
timestamp <- timestamp[!rm]
......@@ -308,8 +316,8 @@ read_ts_tables <- function(file, dir, t.type = "guess",
stop("unknown backend")
if (is.null(return.class)) {
list(data = results,
timestamp = timestamp,
list(data = results,
timestamp = ttime(timestamp, from = "numeric", t.type),
columns = rep(columns, each = length(dfile)),
file.path = paste(rep(dfile, each = length(columns)), columns, sep = "::"))
} else if (return.class == "zoo") {
......@@ -324,15 +332,14 @@ read_ts_tables <- function(file, dir, t.type = "guess",
ans
} else if (return.class == "ts_table") {
ans <- as.matrix(ans)
dimnames(ans) <- NULL
dimnames(ans) <- NULL
attr(ans, "t.type") <- t.type
attr(ans, "timestamp") <- ttime(timestamp)
attr(ans, "columns") <- columns
class(ans) <- "ts_table"
ans
} else {
} else
stop("unknown ", sQuote("return.class"))
}
}
print.ts_table <- function(x, ...) {
......
......@@ -2,9 +2,9 @@
test.ts_table <- function() {
## require("RUnit")
## require("tsdb")
## require("zoo")
require("RUnit")
require("tsdb")
require("zoo")
y <- ts_table(11:15, as.Date("2016-1-1")-5:1, "close")
checkEquals(y,
......
......@@ -13,7 +13,8 @@ read_ts_tables(file, dir, t.type = "guess",
drop.weekends = TRUE,
column.names = "\%dir\%/\%file\%::\%column\%",
backend = "csv",
read.fn = NULL)
read.fn = NULL,
frequency = "1 sec")
}
\arguments{
\item{file}{
......@@ -58,7 +59,12 @@ read_ts_tables(file, dir, t.type = "guess",
to use \code{\link[data.table]{fread}}
from package \pkg{data.table}
}
\item{frequency}{
character: only used when \code{t.type} is
\code{POSIXct} (or guessed to be \code{POSIXct})
}
}
\details{
Read time-series data from csv files.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment