setkey {data.table}R Documentation

Create key on a data table

Description

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.

Usage

setkey(x, ..., loc=parent.frame())

Arguments

x A data.table.
... The columns to sort by. Do not quote the column names. If ... is missing all the columns are used.
loc The data.table must already exist in this frame, and it is sorted by reference in this frame. loc=.GlobalEnv is often useful within functions.

Value

No value is returned. The data.table is modified by reference. If you require a copy, take a copy first. A working copy is currently taken internally.

Author(s)

Matthew Dowle

See Also

data.table, tables, [.data.table, J

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

[Package data.table version 1.2 Index]