diff --git a/R/merge.R b/R/merge.R index 59873816f5..52ad8efd54 100644 --- a/R/merge.R +++ b/R/merge.R @@ -1,4 +1,4 @@ -merge.data.table <- function(x, y, by = NULL, all = FALSE, all.x = all, +merge.data.table <- function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FALSE, all.x = all, all.y = all, suffixes = c(".x", ".y"), allow.cartesian=getOption("datatable.allow.cartesian"), ...) { if (!inherits(y, 'data.table')) { y <- as.data.table(y) @@ -9,6 +9,17 @@ merge.data.table <- function(x, y, by = NULL, all = FALSE, all.x = all, if (any(duplicated(names(x)))) stop("x has some duplicated column name(s): ",paste(names(x)[duplicated(names(x))],collapse=","),". Please remove or rename the duplicate(s) and try again.") if (any(duplicated(names(y)))) stop("y has some duplicated column name(s): ",paste(names(y)[duplicated(names(y))],collapse=","),". Please remove or rename the duplicate(s) and try again.") + ## Determine by and rename columns of y, if by.x and by.y are supplied + if (!is.null(by.x)){ + if (!is.null(by)) warning("Supplied both by and by.x and only by will be used") + else { + by <- by.x + if (length(by.x) != length(by.y)) stop("by.x and by.y must be of the same length") + setnames(y, by.y, by.x) + on.exit(setnames(y, by.x, by.y)) + } + } + ## Try to infer proper value for `by` if (is.null(by)) { by <- intersect(key(x), key(y)) diff --git a/README.md b/README.md index 0ec9864be9..a849070867 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ - + **Current stable release** (always even) : [v1.9.4 on CRAN](http://cran.r-project.org/web/packages/data.table/index.html), released 2nd Oct 2014. **Development version** (always odd): [v1.9.5 on GitHub](https://github.com/Rdatatable/data.table/) [![Build Status](https://travis-ci.org/Rdatatable/data.table.svg?branch=master)](https://travis-ci.org/Rdatatable/data.table) [![codecov.io](http://codecov.io/github/Rdatatable/data.table/coverage.svg?branch=master)](http://codecov.io/github/Rdatatable/data.table?branch=master) [How to install?](https://github.com/Rdatatable/data.table/wiki/Installation) @@ -63,6 +63,8 @@ 21. `setDF` also converts `list` of equal length to `data.frame` by reference now. Closes [#1132](https://github.com/Rdatatable/data.table/issues/1132). + 22. `merge.data.table` now has new arguments `by.x` and `by.y`. Closes [#637](https://github.com/Rdatatable/data.table/issues/637). Thanks to @NelloBlaser. + #### BUG FIXES 1. `if (TRUE) DT[,LHS:=RHS]` no longer prints, [#869](https://github.com/Rdatatable/data.table/issues/869). Tests added. To get this to work we've had to live with one downside: if a `:=` is used inside a function with no `DT[]` before the end of the function, then the next time `DT` is typed at the prompt, nothing will be printed. A repeated `DT` will print. To avoid this: include a `DT[]` after the last `:=` in your function. If that is not possible (e.g., it's not a function you can change) then `print(DT)` and `DT[]` at the prompt are guaranteed to print. As before, adding an extra `[]` on the end of `:=` query is a recommended idiom to update and then print; e.g. `> DT[,foo:=3L][]`. Thanks to Jureiss for reporting. diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index b4df64fe76..f4d4a069fe 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -6387,6 +6387,13 @@ x = data.table(k = INT(0,2,3,7), o = "b", key = "k") y = data.table(k = 1:5, n = paste("n", 1:5, sep=""), key = "k") test(1519.2, x[y, o := n, nomatch = 0], data.table(k = INT(0,2,3,7), o = c("b","n2","n3","b"), key = "k")) +# #637 add by.x and by.y to merge.data.table +d1 <- data.table(x1=c(1,3,8), y1=rnorm(3), key="x1") +d2 <- data.table(x2=c(3,8,10), y2=rnorm(3), key="x2") +ans1 = merge(d1, d2, by.x = "x1", by.y = "x2") +ans2 = setkey(setDT(merge.data.frame(d1, d2, by.x = key(d1), by.y = key(d2))), x1) +test(1520, ans1, ans2) + ########################## diff --git a/man/merge.Rd b/man/merge.Rd index 1cefe88dc4..c8e85004cf 100644 --- a/man/merge.Rd +++ b/man/merge.Rd @@ -13,7 +13,7 @@ } \usage{ -\method{merge}{data.table}(x, y, by = NULL, all = FALSE, all.x = all, all.y = all, suffixes = c(".x", ".y"), +\method{merge}{data.table}(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FALSE, all.x = all, all.y = all, suffixes = c(".x", ".y"), allow.cartesian=getOption("datatable.allow.cartesian"), # default FALSE ...) } @@ -29,6 +29,10 @@ allow.cartesian=getOption("datatable.allow.cartesian"), # default FALSE This defaults to the shared key columns between the two tables. If \code{y} has no key columns, this defaults to the key of \code{x}. } + + \item{by.x, by.y}{ + Vectors of column names in \code{x} and \code{y} to merge on. + } \item{all}{ logical; \code{all = TRUE} is shorthand to save setting both \code{all.x = TRUE} and