I am trying to do a full Cartesian join using data.table but with little luck.
Code:
a = data.table(dt=c(20131017,20131018))
setkey(a,dt)
b = data.table(ticker=c("ABC","DEF","XYZ"),ind=c("MISC1","MISC2","MISC3"))
setkey(b,ticker)
Expected output:
merge(data.frame(a),data.frame(b),all.x=TRUE,all.y=TRUE)
I have tried merge(a,b,allow.cartesian=TRUE)
but it gives me following error - "Error in merge.data.table(a, b, allow.cartesian = TRUE) : A non-empty vector of column names for
byis required.
"
I am using "R version 3.0.1 (2013-05-16)
" with latest data.table
packages. Any help would be greatly appreciated!
Regards
expand.grid
-like problem than amerge
-problem. You have no common variable. – IRTFM Oct 18 '13 at 2:48allow.cartesian = TRUE
.CJ
andexpand.grid
can cross join vectors but I could never find a base function to create a cross-join of two tables. Can anybody point to such a function, if it exists? – TheComeOnMan Oct 18 '13 at 3:20CJ
orexpand.grid
of row numbers; never have found a better way. Here's an alternative, clumsy, solution:data.table(merge.data.frame(a,b,all=TRUE))
. – Frank Oct 18 '13 at 3:55merge
operation. However to be compatible withmerge.data.frame
, I guess this functionality is required. Would you mind filing a feature request here? Thank you. – Arun Oct 18 '13 at 4:24