setkey

0th

Percentile

Create key on a data table

Sorts a data.table and marks it as sorted. The sorted columns are the key. The key can be any columns in any order. The columns are sorted in ascending order always.

Keywords
data
Usage
setkey(x, ..., loc=parent.frame(),verbose=getOption("datatable.verbose",FALSE))
key(x)
key(x) <- value
haskey(x)
copy(x)
Arguments
x
An unquoted name of a data.table.
...
The columns to sort by. Do not quote the column names. If ... is missing all the columns are used.
value
A character vector of column names.
loc
The data.table must already exist in this frame and is sorted by reference in this frame. loc=.GlobalEnv is often useful within functions.
verbose
Output status and information.
Details

The sort is attempted with the very fast "radix" method in sort.list. If that fails, the sort reverts to the default method in order. That logic is repeated column by column. The sort is stable; i.e., the order of ties (if any) is preserved. If value=NULL, the key is removed.

Value

  • No value is returned. The data.table is modified by reference. If you require a copy, take a copy first (using DT2=copy(DT)). copy() may also sometimes be useful before := is used to subassign to a column by reference.

Note

base::sort.list(x,method="radix") actually invokes a counting sort, not a radix sort. See do_radixsort in src/main/sort.c. A counting sort is particularly suitable for sorting integers and factors, however, and we like it. Anyway, this is one reason data.table 'likes' integers and factors.

References

http://en.wikipedia.org/wiki/Radix_sort http://en.wikipedia.org/wiki/Counting_sort

See Also

data.table, tables, J, sort.list html{

web statistics
}

Aliases
  • setkey
  • key
  • key<-
  • haskey
  • copy
Examples
DT = data.table(A=5:1,B=letters[5:1])
    DT # before
    setkey(DT,B)  # re-orders table and marks it sorted.
    DT # after
    tables()      # KEY column reports the key'd columns
    key(DT)
    key(DT) = "A"
    
    DT = data.table(A=5:1,B=letters[5:1])
    DT2 = DT              # not enough to copy
    setkey(DT2,B)         # does not copy on write to DT2
    identical(DT,DT2)     # TRUE. DT and DT2 are two names for the same keyed table
    
    DT = data.table(A=5:1,B=letters[5:1])
    DT2 = copy(DT)        # explicit copy is required for data.table
    setkey(DT2,B)         # just changes DT2
    identical(DT,DT2)     # FALSE. DT and DT2 are now different tables
Documentation reproduced from package data.table, version 1.7.6, License: GPL (>= 2)

Community examples

raf.v.asuncion@gmail.com at Feb 27, 2018 data.table v1.10.4-2

DT = data.table(A=5:1,B=letters[5:1]) DT # before setkey(DT,B) # re-orders table and marks it sorted. DT # after