Skip to content

Commit

Permalink
Closes #1121. setDT gains 'key' argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan committed Apr 29, 2015
1 parent 426e8e4 commit 765111b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ setDF <- function(x) {
invisible(x)
}

setDT <- function(x, keep.rownames=FALSE) {
setDT <- function(x, keep.rownames=FALSE, key=NULL) {
name = substitute(x)
if (is.name(name)) {
home <- function(x, env) {
Expand Down Expand Up @@ -2440,6 +2440,7 @@ setDT <- function(x, keep.rownames=FALSE) {
} else {
stop("Argument 'x' to 'setDT' should be a 'list', 'data.frame' or 'data.table'")
}
if (!is.null(key)) setkeyv(x, key)
if (is.name(name)) {
name = as.character(name)
assign(name, x, parent.frame(), inherits=TRUE)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

19. `as.data.table.*` and `setDT` argument `keep.rownames` can take a column name as well. When `keep.rownames=TRUE`, the column will still automatically named `rn`. Closes [#575](https://github.com/Rdatatable/data.table/issues/575).

20. `setDT` gains a `key` argument so that `setDT(X, key="a")` would convert `X` to a `data.table` by reference *and* key by the columns specified. Closes [#1121](https://github.com/Rdatatable/data.table/issues/1121).

#### BUG FIXES

1. `if (TRUE) DT[,LHS:=RHS]` no longer prints, [#869](https://github.com/Rdatatable/data.table/issues/869). Tests added. To get this to work we've had to live with one downside: if a `:=` is used inside a function with no `DT[]` before the end of the function, then the next time `DT` is typed at the prompt, nothing will be printed. A repeated `DT` will print. To avoid this: include a `DT[]` after the last `:=` in your function. If that is not possible (e.g., it's not a function you can change) then `print(DT)` and `DT[]` at the prompt are guaranteed to print. As before, adding an extra `[]` on the end of `:=` query is a recommended idiom to update and then print; e.g. `> DT[,foo:=3L][]`. Thanks to Jureiss for reporting.
Expand Down
4 changes: 4 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -6307,6 +6307,10 @@ if ("package:bit64" %in% search()) {
test(1512.6, dt[.(as.integer64(2))], dt[.(2L)])
}

# setDT gains key argument, #1121
X = list(a = 4:1, b=runif(4))
test(1513, setkey(as.data.table(X), a), setDT(X, key="a"))

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


Expand Down
7 changes: 6 additions & 1 deletion man/setDT.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

}
\usage{
setDT(x, keep.rownames=FALSE)
setDT(x, keep.rownames=FALSE, key=NULL)
}
\arguments{
\item{x}{ A named or unnamed \code{list}, \code{data.frame} or \code{data.table}. }
\item{keep.rownames}{ For \code{data.frame}s, \code{TRUE} retains the \code{data.frame}'s row names under a new column \code{rn}. }
\item{key}{Character vector of one or more column names which is passed to \code{\link{setkeyv}}. It may be a single comma separated string such as \code{key="x,y,z"}, or a vector of names such as \code{key=c("x","y","z")}. }
}
\details{
Expand Down Expand Up @@ -43,6 +44,10 @@ setDT(X)
# don't provide names
X = list(a=1:4, letters[1:4])
setDT(X, FALSE)

# setkey directly
X = list(a = 4:1, b=runif(4))
setDT(X, key="a")[]
}
\keyword{ data }

0 comments on commit 765111b

Please sign in to comment.