data.table (version 1.9.6)

J: Creates a Join data table

Description

Creates a data.table to be passed in as the i to a [.data.table join.

Usage

# DT[J(...)] # J() only for use inside DT[...]. SJ(...) # DT[SJ(...)] CJ(..., sorted = TRUE, unique = FALSE) # DT[CJ(...)]

Arguments

...
Each argument is a vector. Generally each vector is the same length but if they are not then usual silent repitition is applied.
sorted
logical. Should the input order be retained?
unique
logical. When TRUE, only unique values of each vectors are used (automatically).

Value

Details

SJ and CJ are convenience functions for creating a data.table in the context of a data.table 'query' on x.

x[data.table(id)] is the same as x[J(id)] but the latter is more readable. Identical alternatives are x[list(id)] and x[.(id)].

x must have a key when passing in a join table as the i. See [.data.table

See Also

data.table, test.data.table

Examples

Run this code
DT = data.table(A=5:1,B=letters[5:1])
setkey(DT,B)    # re-orders table and marks it sorted.
DT[J("b")]      # returns the 2nd row
DT[.("b")]      # same. Style of package plyr.
DT[list("b")]   # same

# CJ usage examples
CJ(c(5,NA,1), c(1,3,2)) # sorted and keyed data.table
do.call(CJ, list(c(5,NA,1), c(1,3,2))) # same as above
CJ(c(5,NA,1), c(1,3,2), sorted=FALSE) # same order as input, unkeyed 
# use for 'unique=' argument
x = c(1,1,2)
y = c(4,6,4)
CJ(x, y, unique=TRUE) # unique(x) and unique(y) are computed automatically

Run the code above in your browser using DataCamp Workspace