From 1b9643837890b7669d8ca2ee04d3c44aff9e1bcb Mon Sep 17 00:00:00 2001 From: stephlocke Date: Tue, 16 Sep 2014 20:44:43 +0100 Subject: [PATCH] Create CJ.dt Done as separate code on the assumption that it can later be deprecated when better code is integrated into CJ to handle two data.tables --- R/CJ.dt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 R/CJ.dt diff --git a/R/CJ.dt b/R/CJ.dt new file mode 100644 index 000000000..d72e1d4fb --- /dev/null +++ b/R/CJ.dt @@ -0,0 +1,26 @@ +#' Cross join two data.tables +#' +#' The package data.table has a CJ() function which produces a data.table out of two vectors. +#' This function does the Cartesian product of two data.tables instead. +#' +#' @param X A data.table +#' @param Y A data.table +#' @return dt A data.table +#' +#' @keywords data.table CJ +#' @family helper +#' +#' @examples +#' library(data.table) +#' a <- data.table(a=1:2, b=letters[1:2]) +#' b <- data.table(c=3:4, d=letters[3:4]) +#' ab <- CJ.dt(a,b) +#' +#' @export +#' + +CJ.dt<-function(X,Y) { + stopifnot(is.data.table(X),is.data.table(Y)) + k <- NULL # Setting the variables to NULL first for CRAN check NOTE + setkey(X[,c(k=1,.SD)],k)[Y[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL] +}