Data.table 中的交叉连接似乎不保留列名

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

data.table 文档是这样说的,请参阅 ?CJ:

x = c(1,1,2)

y = c(4,6,4)

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

但是当我运行这个例子时,它似乎没有被保留

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

帮助文件 ?CJ 的正文中未提及保留名称,即在“详细信息”或“值”部分中。但是,似乎提到在帮助文件的示例部分中保留名称作为注释(看起来这就是您获得示例的地方)。

CJ 函数中挖掘,似乎完全在 R 中实现,在末尾有一个块,

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

运行 getOption("datatable.CJ.names", FALSE) returns FALSE data.table 版本 1.12.0。当我们使用

将其设置为 TRUE 时
options("datatable.CJ.names"=TRUE)

然后是代码

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

不过,您也可以直接提供名称(帮助文件中未提及)。

CJ(uu=x, vv=y)

哪个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

请注意,这会覆盖上述选项。