R/cross-join.R

Defines functions cross_join.data.frame cross_join.tbl_sql cross_join

Documented in cross_join

#' Cross join two tables together, including all combinations of rows
#'
#' Locally, this is equivalent to tidyr::crossing.
#'
#' @param x,y tbls to join
#' @param ... additional arguments to be passed on to
#'   [dplyr::full_join()] or [tidyr::crossing()]
#' @export
#' @examples
#' d1 <- dplyr::tibble(x = 1:3)
#' d2 <- dplyr::tibble(y = 1:2)
#' cross_join(d1, d2)
cross_join <- function(x, y, ...) {
  UseMethod("cross_join", x)
}

#' @export
cross_join.tbl_sql <- function(x, y, ...) {
  dplyr::full_join(x, y, by = character())
}

#' @export
cross_join.data.frame <- function(x, y, ...) {
  tidyr::crossing(x, y, ...)
}
datacamp/tidymetrics documentation built on March 21, 2021, 3:28 a.m.