J: Creates a Join data table

Description Usage Arguments Details Value See Also Examples

Description

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

Usage

1
2
3
# DT[J(...)]            # J() only for use inside DT[...].
SJ(...)                 # DT[SJ(...)]
CJ(..., sorted = TRUE)  # 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?

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

Value

J : the same result as calling list. J is a direct alias for list but results in clearer more readable code. SJ : (S)orted (J)oin. The same value as J() but additionally setkey() is called on all the columns in the order they were passed in to SJ. For efficiency, to invoke a binary merge rather than a repeated binary full search for each row of i. CJ : (C)ross (J)oin. A data.table is formed from the cross product of the vectors. For example, 10 ids, and 100 dates, CJ returns a 1000 row table containing all the dates for all the ids. It gains sorted, which by default is TRUE for backwards compatibility. FALSE retains input order.

See Also

data.table, test.data.table

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    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 

Example output

   A B
1: 2 b
   A B
1: 2 b
   A B
1: 2 b
   V1 V2
1: NA  1
2: NA  2
3: NA  3
4:  1  1
5:  1  2
6:  1  3
7:  5  1
8:  5  2
9:  5  3
   V1 V2
1: NA  1
2: NA  2
3: NA  3
4:  1  1
5:  1  2
6:  1  3
7:  5  1
8:  5  2
9:  5  3
   V1 V2
1:  5  1
2:  5  3
3:  5  2
4: NA  1
5: NA  3
6: NA  2
7:  1  1
8:  1  3
9:  1  2

data.table documentation built on May 2, 2019, 4:57 p.m.