-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xts-data.table conversion, closes #882
- Loading branch information
1 parent
ea2a006
commit 73d5b4a
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
as.data.table.xts <- function(x, keep.rownames = TRUE){ | ||
stopifnot(requireNamespace("xts"), !missing(x), xts::is.xts(x)) | ||
if(!keep.rownames) return(setDT(as.data.frame(x, row.names=FALSE))[]) | ||
if("index" %in% names(x)) stop("Input xts object should not have 'index' column because it would result in duplicate column names. Rename 'index' column in xts or use `keep.rownames=FALSE` and add index manually as another column.") | ||
r = setDT(as.data.frame(x, row.names=FALSE)) | ||
r[, index := xts:::index.xts(x)] | ||
setcolorder(r,c("index",names(r)[names(r)!="index"]))[] | ||
} | ||
|
||
as.xts.data.table <- function(x){ | ||
stopifnot(requireNamespace("xts"), !missing(x), is.data.table(x)) | ||
if(!any(class(x[[1]]) %in% c("POSIXct","Date"))) stop("data.table must have a POSIXct or Date column on first position, use `setcolorder` function.") | ||
colsNumeric = sapply(x, is.numeric)[-1] # exclude first col, xts index | ||
if(any(!colsNumeric)) warning(paste("Following columns are not numeric and will be omitted:",paste(names(colsNumeric)[!colsNumeric],collapse=", "))) | ||
r = setDF(x[,.SD,.SDcols=names(colsNumeric)[colsNumeric]]) | ||
xts::as.xts(r, order.by=x[[1]]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
\name{as.data.table.xts} | ||
\alias{as.data.table.xts} | ||
\title{Efficient xts to as.data.table conversion} | ||
\description{ | ||
Efficient conversion xts to data.table. | ||
} | ||
\usage{ | ||
\method{as.data.table}{xts}(x, keep.rownames = TRUE) | ||
} | ||
\arguments{ | ||
\item{x}{xts to convert to data.table} | ||
|
||
\item{keep.rownames}{keep xts index as \emph{index} column in result data.table} | ||
} | ||
\seealso{ \code{\link{as.xts.data.table}} } | ||
\examples{ | ||
\dontrun{ | ||
data(sample_matrix, package = "xts") | ||
sample.xts <- xts::as.xts(sample_matrix) # xts might not be attached on search path | ||
# print head of xts | ||
print(head(sample.xts)) | ||
# print dt | ||
print(as.data.table(sample.xts)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
\name{as.xts.data.table} | ||
\alias{as.xts.data.table} | ||
\title{Efficient data.table to xts conversion} | ||
\description{ | ||
Efficient conversion of data.table to xts, data.table must have \emph{POSIXct} or \emph{Date} type in first column. | ||
} | ||
\usage{ | ||
as.xts.data.table(x) | ||
} | ||
\arguments{ | ||
\item{x}{data.table to convert to xts, must have \emph{POSIXct} or \emph{Date} in the first column. All others non-numeric columns will be omitted with warning.} | ||
} | ||
\seealso{ \code{\link{as.data.table.xts}} } | ||
\examples{ | ||
\dontrun{ | ||
sample.dt <- data.table(date = as.Date((Sys.Date()-999):Sys.Date(),origin="1970-01-01"), | ||
quantity = sample(10:50,1000,TRUE), | ||
value = sample(100:1000,1000,TRUE)) | ||
# print dt | ||
print(sample.dt) | ||
# print head of xts | ||
print(head(as.xts.data.table(sample.dt))) # xts might not be attached on search path | ||
} | ||
} |