Join sql tbls.

See join for a description of the general purpose of the functions.

# S3 method for tbl_sql
inner_join(x, y, by = NULL, copy = FALSE,
  auto_index = FALSE, ...)

# S3 method for tbl_sql left_join(x, y, by = NULL, copy = FALSE, auto_index = FALSE, ...)

# S3 method for tbl_sql semi_join(x, y, by = NULL, copy = FALSE, auto_index = FALSE, ...)

# S3 method for tbl_sql anti_join(x, y, by = NULL, copy = FALSE, auto_index = FALSE, ...)

tbls to join
a character vector of variables to join by. If NULL, the default, join will do a natural join, using all variables with common names across the two tables. A message lists the variables so that you can check they're right.

To join by different variables on x and y use a named vector. For example, by = c("a" = "b") will match x.a to y.b.

If x and y are not from the same data source, and copy is TRUE, then y will be copied into a temporary table in same database as x. join will automatically run ANALYZE on the created table in the hope that this will make you queries as efficient as possible by giving more data to the query planner.

This allows you to join tables across srcs, but it's potentially expensive operation so you must opt into it.

if copy is TRUE, automatically create indices for the variables in by. This may speed up the join if there are matching indexes in x.
other parameters passed onto methods
Implementation notes

Semi-joins are implemented using WHERE EXISTS, and anti-joins with WHERE NOT EXISTS. Support for semi-joins is somewhat partial: you can only create semi joins where the x and y columns are compared with = not with more general operators.

  • anti_join.tbl_sql
  • inner_join.tbl_sql
  • join.tbl_sql
  • left_join.tbl_sql
  • semi_join.tbl_sql
if (require("RSQLite") && has_lahman("sqlite")) {

# Left joins ----------------------------------------------------------------
batting <- tbl(lahman_sqlite(), "Batting")
team_info <- select(tbl(lahman_sqlite(), "Teams"), yearID, lgID, teamID, G, R:H)

# Combine player and whole team statistics
first_stint <- select(filter(batting, stint == 1), playerID:H)
both <- left_join(first_stint, team_info, type = "inner", by = c("yearID", "teamID", "lgID"))

# Join with a local data frame
grid <- expand.grid(
  teamID = c("WAS", "ATL", "PHI", "NYA"),
  yearID = 2010:2012)
top4a <- left_join(batting, grid, copy = TRUE)

# Indices don't really help here because there's no matching index on
# batting
top4b <- left_join(batting, grid, copy = TRUE, auto_index = TRUE)

# Semi-joins ----------------------------------------------------------------

people <- tbl(lahman_sqlite(), "Master")

# All people in half of fame
hof <- tbl(lahman_sqlite(), "HallOfFame")
semi_join(people, hof)

# All people not in the hall of fame
anti_join(people, hof)

# Find all managers
manager <- tbl(lahman_sqlite(), "Managers")
semi_join(people, manager)

# Find all managers in hall of fame
famous_manager <- semi_join(semi_join(people, manager), hof)

# Anti-joins ----------------------------------------------------------------

# batters without person covariates
anti_join(batting, people)

Documentation reproduced from package dplyr, version 0.3, License: MIT + file LICENSE

Community examples

Looks like there are no examples yet.