ashleych
ashleych

Reputation: 1054

Cross join in Data.table doesnt seem to retain column names

data.table documentation says this, see ?CJ:

x = c(1,1,2)

y = c(4,6,4)

CJ(x, y) # output columns are automatically named 'x' and 'y'

However when I run the example, it doesnt seem to be retained

x = c(1,1,2)
y = c(4,6,4)
CJ(x, y)
   V1 V2
1:  1  4
2:  1  4
3:  1  4
4:  1  4
5:  1  6
6:  1  6
7:  2  4
8:  2  4
9:  2  6

Upvotes: 4

Views: 280

Answers (1)

lmo
lmo

Reputation: 38500

That names are retained is not mentioned in the main body of the help file ?CJ, that is in the Details or Value section. However, there appears to be mention that names are retained as a comment in the examples section of the help file (and it looks like this is where you got your example).

Digging around in the CJ function, which appears to be entirely implemented in R, there is a block near the end,

if (getOption("datatable.CJ.names", FALSE))
    vnames = name_dots(...)$vnames

Running getOption("datatable.CJ.names", FALSE) returns FALSE with data.table version 1.12.0. When we set this to TRUE with

options("datatable.CJ.names"=TRUE)

then the code

x = c(1,1,2)
y = c(4,6,4)

CJ(x, y)

returns

   x y
1: 1 4
2: 1 4
3: 1 4
4: 1 4
5: 1 6
6: 1 6
7: 2 4
8: 2 4
9: 2 6

However, you are also able to directly provide names (which is not mentioned in the help file).

CJ(uu=x, vv=y)

which returns

   uu vv
1:  1  4
2:  1  4
3:  1  4
4:  1  4
5:  1  6
6:  1  6
7:  2  4
8:  2  4
9:  2  6

Note that this overrides the above option.

Upvotes: 2

Related Questions