Skip to content

Commit

Permalink
Merge pull request #999 from jangorecki/as.data.table.default
Browse files Browse the repository at this point in the history
Closes #969, implemented as.data.table.default method.
  • Loading branch information
arunsrinivasan committed Jan 10, 2015
2 parents a18844e + 473c754 commit a0d2ca2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ S3method(as.data.table, ordered)
S3method(as.data.table, Date)
S3method(as.data.table, table)
S3method(as.data.table, xts)
S3method(as.data.table, default)
S3method(as.data.frame, data.table)
S3method(as.list, data.table)
S3method(as.matrix, data.table)
Expand Down
18 changes: 11 additions & 7 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ as.matrix.data.table <- function(x,...)
X
}

as.data.table.matrix <- function(x, keep.rownames=FALSE)
as.data.table.matrix <- function(x, keep.rownames=FALSE, ...)
{
if (keep.rownames) return(data.table(rn=rownames(x), x, keep.rownames=FALSE))
d <- dim(x)
Expand Down Expand Up @@ -1661,7 +1661,7 @@ as.data.table.matrix <- function(x, keep.rownames=FALSE)
alloc.col(value)
}

as.data.table.data.frame <- function(x, keep.rownames=FALSE)
as.data.table.data.frame <- function(x, keep.rownames=FALSE, ...)
{
if (keep.rownames) return(data.table(rn=rownames(x), x, keep.rownames=FALSE))
ans = copy(x) # TO DO: change this deep copy to be shallow.
Expand All @@ -1675,7 +1675,7 @@ as.data.table.data.frame <- function(x, keep.rownames=FALSE)
alloc.col(ans)
}

as.data.table.list <- function(x, keep.rownames=FALSE) {
as.data.table.list <- function(x, keep.rownames=FALSE, ...) {
if (!length(x)) return( null.data.table() )
n = vapply(x, length, 0L)
mn = max(n)
Expand All @@ -1697,13 +1697,13 @@ as.data.table.list <- function(x, keep.rownames=FALSE) {
alloc.col(x)
}

as.data.table.data.table <- function(x, keep.rownames=FALSE) return(x)
as.data.table.data.table <- function(x, keep.rownames=FALSE, ...) return(x)

# takes care of logical, character, numeric, integer
as.data.table.factor <- as.data.table.ordered <-
as.data.table.integer <- as.data.table.numeric <-
as.data.table.logical <- as.data.table.character <-
as.data.table.Date <- function(x, keep.rownames=FALSE) {
as.data.table.Date <- function(x, keep.rownames=FALSE, ...) {
tt = deparse(substitute(x))[1]
nm = names(x)
# FR #2356 - transfer names of named vector as "rn" column if required
Expand Down Expand Up @@ -1734,7 +1734,7 @@ R300_provideDimnames <- function (x, sep = "", base = list(LETTERS)) # backpor
}

# as.data.table.table - FR #4848
as.data.table.table <- function(x, keep.rownames=FALSE) {
as.data.table.table <- function(x, keep.rownames=FALSE, ...) {
# Fix for bug #5408 - order of columns are different when doing as.data.table(with(DT, table(x, y)))
val = rev(dimnames(R300_provideDimnames(x)))
if (is.null(names(val)) || all(nchar(names(val)) == 0L))
Expand Down Expand Up @@ -1836,13 +1836,17 @@ tail.data.table <- function(x, n=6, ...) {
`[<-.data.table`(x,j=name,value=value) # important i is missing here
}

as.data.table <-function(x, keep.rownames=FALSE)
as.data.table <-function(x, keep.rownames=FALSE, ...)
{
if (is.null(x))
return(null.data.table())
UseMethod("as.data.table")
}

as.data.table.default <- function(x, ...){
setDT(as.data.frame(x, ...))[]
}

as.data.frame.data.table <- function(x, ...)
{
ans = copy(x)
Expand Down
2 changes: 1 addition & 1 deletion R/xts.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
as.data.table.xts <- function(x, keep.rownames = TRUE){
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.")
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -5741,6 +5741,12 @@ if ("package:xts" %in% search()) {
test(1465.4, xt, xt_dt)
}

# as.data.table.default #969
ar <- array(NA, dim=c(10,4),dimnames = list(NULL,paste0("col",1:4)))
test(1466.1, as.data.table(as.data.frame(ar)), as.data.table(ar)) # array type
x <- rep(Sys.time(),3)
test(1466.2, as.data.table(as.data.frame(x)), as.data.table(x)) # posix type

##########################


Expand Down
4 changes: 3 additions & 1 deletion man/as.data.table.xts.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
Efficient conversion xts to data.table.
}
\usage{
\method{as.data.table}{xts}(x, keep.rownames = TRUE)
\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}

\item{\dots}{ignored, just for consistency with \code{as.data.table}}
}
\seealso{ \code{\link{as.xts.data.table}} }
\examples{
Expand Down

0 comments on commit a0d2ca2

Please sign in to comment.