merge {joyn} | R Documentation |
Merge two data frames
Description
This is a joyn wrapper that works in a similar fashion to base::merge and data.table::merge, which is why merge masks the other two.
Usage
merge(
x,
y,
by = NULL,
by.x = NULL,
by.y = NULL,
all = FALSE,
all.x = all,
all.y = all,
sort = TRUE,
suffixes = c(".x", ".y"),
no.dups = TRUE,
allow.cartesian = getOption("datatable.allow.cartesian"),
match_type = c("m:m", "m:1", "1:m", "1:1"),
keep_common_vars = TRUE,
...
)
Arguments
x , y |
|
by |
A vector of shared column names in |
by.x , by.y |
Vectors of column names in |
all |
logical; |
all.x |
logical; if |
all.y |
logical; analogous to |
sort |
logical. If |
suffixes |
A |
no.dups |
logical indicating that |
allow.cartesian |
See |
match_type |
character: one of "m:m", "m:1", "1:m", "1:1". Default is "1:1" since this the most restrictive. However, following Stata's recommendation, it is better to be explicit and use any of the other three match types (See details in match types sections). |
keep_common_vars |
logical: If TRUE, it will keep the original variable from y when both tables have common variable names. Thus, the prefix "y." will be added to the original name to distinguish from the resulting variable in the joined table. |
... |
Arguments passed on to
|
Value
data.table merging x and y
Examples
x1 = data.frame(id = c(1L, 1L, 2L, 3L, NA_integer_),
t = c(1L, 2L, 1L, 2L, NA_integer_),
x = 11:15)
y1 = data.frame(id = c(1,2, 4),
y = c(11L, 15L, 16))
joyn::merge(x1, y1, by = "id")
# example of using by.x and by.y
x2 = data.frame(id1 = c(1, 1, 2, 3, 3),
id2 = c(1, 1, 2, 3, 4),
t = c(1L, 2L, 1L, 2L, NA_integer_),
x = c(16, 12, NA, NA, 15))
y2 = data.frame(id = c(1, 2, 5, 6, 3),
id2 = c(1, 1, 2, 3, 4),
y = c(11L, 15L, 20L, 13L, 10L),
x = c(16:20))
jn <- joyn::merge(x2,
y2,
match_type = "m:m",
all.x = TRUE,
by.x = "id1",
by.y = "id2")
# example with all = TRUE
jn <- joyn::merge(x2,
y2,
match_type = "m:m",
by.x = "id1",
by.y = "id2",
all = TRUE)