這些是 dplyr 泛型 left_join()
、 right_join()
、 inner_join()
、 full_join()
、 anti_join()
和 semi_join()
的方法。左、右、內和反連接被轉換為 [.data.table
等效的完全連接到 data.table::merge.data.table()
。在某些情況下,左連接、右連接和全連接後麵會調用 data.table::setcolorder()
和 data.table::setnames()
,以確保列順序和名稱與 dplyr 約定匹配。半連接沒有直接的 data.table 等效項。
用法
# S3 method for dtplyr_step
left_join(x, y, ..., by = NULL, copy = FALSE, suffix = c(".x", ".y"))
參數
- x, y
-
一對
lazy_dt()
。 - ...
-
傳遞給方法的其他參數。
- by
-
使用
join_by()
創建的連接規範,或要連接的變量的字符向量。如果
NULL
(默認值),*_join()
將使用x
和y
之間的所有共同變量執行自然連接。一條消息列出了變量,以便您可以檢查它們是否正確;通過顯式提供by
來抑製該消息。要連接
x
和y
之間的不同變量,請使用join_by()
規範。例如,join_by(a == b)
將匹配x$a
到y$b
。要連接多個變量,請使用帶有多個表達式的
join_by()
規範。例如,join_by(a == b, c == d)
將x$a
與y$b
匹配,將x$c
與y$d
匹配。如果x
和y
之間的列名稱相同,您可以通過僅列出變量名稱來縮短列名稱,例如join_by(a, c)
。join_by()
還可用於執行不等式連接、滾動連接和重疊連接。有關這些類型的連接的詳細信息,請參閱?join_by 中的文檔。對於簡單的等式連接,您也可以指定要連接的變量名稱的字符向量。例如,
by = c("a", "b")
將x$a
連接到y$a
並將x$b
連接到y$b
。如果x
和y
之間的變量名稱不同,請使用命名字符向量,例如by = c("x_a" = "y_a", "x_b" = "y_b")
。要執行交叉聯接,生成
x
和y
的所有組合,請參閱cross_join()
。 - copy
-
如果
x
和y
不是來自同一個數據源,並且copy
是TRUE
,則y
將被複製到與x
相同的源中。這允許您跨 src 連接表,但這是一項潛在昂貴的操作,因此您必須選擇它。 - suffix
-
如果
x
和y
中存在未連接的重複變量,這些後綴將添加到輸出中以消除它們的歧義。應該是長度為 2 的字符向量。
例子
library(dplyr, warn.conflicts = FALSE)
band_dt <- lazy_dt(dplyr::band_members)
instrument_dt <- lazy_dt(dplyr::band_instruments)
band_dt %>% left_join(instrument_dt)
#> Joining, by = "name"
#> Source: local data table [3 x 3]
#> Call: setcolorder(`_DT23`[`_DT22`, on = .(name), allow.cartesian = TRUE],
#> c(1L, 3L, 2L))
#>
#> name band plays
#> <chr> <chr> <chr>
#> 1 Mick Stones NA
#> 2 John Beatles guitar
#> 3 Paul Beatles bass
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
band_dt %>% right_join(instrument_dt)
#> Joining, by = "name"
#> Source: local data table [3 x 3]
#> Call: `_DT22`[`_DT23`, on = .(name), allow.cartesian = TRUE]
#>
#> name band plays
#> <chr> <chr> <chr>
#> 1 John Beatles guitar
#> 2 Paul Beatles bass
#> 3 Keith NA guitar
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
band_dt %>% inner_join(instrument_dt)
#> Joining, by = "name"
#> Source: local data table [2 x 3]
#> Call: `_DT22`[`_DT23`, on = .(name), nomatch = NULL, allow.cartesian = TRUE]
#>
#> name band plays
#> <chr> <chr> <chr>
#> 1 John Beatles guitar
#> 2 Paul Beatles bass
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
band_dt %>% full_join(instrument_dt)
#> Joining, by = "name"
#> Source: local data table [4 x 3]
#> Call: merge(`_DT22`, `_DT23`, all = TRUE, by.x = "name", by.y = "name",
#> allow.cartesian = TRUE)
#>
#> name band plays
#> <chr> <chr> <chr>
#> 1 John Beatles guitar
#> 2 Keith NA guitar
#> 3 Mick Stones NA
#> 4 Paul Beatles bass
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
band_dt %>% semi_join(instrument_dt)
#> Joining, by = "name"
#> Source: local data table [2 x 2]
#> Call: `_DT22`[unique(`_DT22`[`_DT23`, which = TRUE, nomatch = NULL,
#> on = .(name)])]
#>
#> name band
#> <chr> <chr>
#> 1 John Beatles
#> 2 Paul Beatles
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
band_dt %>% anti_join(instrument_dt)
#> Joining, by = "name"
#> Source: local data table [1 x 2]
#> Call: `_DT22`[!`_DT23`, on = .(name)]
#>
#> name band
#> <chr> <chr>
#> 1 Mick Stones
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
相關用法
- R dtplyr lazy_dt 創建一個“惰性”data.table 以與 dplyr 動詞一起使用
- R dtplyr group_modify.dtplyr_step 對每個組應用一個函數
- R dtplyr transmute.dtplyr_step 創建新列,刪除舊列
- R dtplyr slice.dtplyr_step 使用行的位置對行進行子集化
- R dtplyr fill.dtplyr_step 用上一個或下一個值填充缺失值
- R dtplyr filter.dtplyr_step 使用列值對行進行子集化
- R dtplyr mutate.dtplyr_step 創建和修改列
- R dtplyr distinct.dtplyr_step 子集不同/唯一行
- R dtplyr unite.dtplyr_step 通過將字符串粘貼在一起將多列合並為一列。
- R dtplyr nest.dtplyr_step 巢
- R dtplyr relocate.dtplyr_step 使用變量名稱重新定位變量
- R dtplyr head.dtplyr_step 對第一行或最後一行進行子集化
- R dtplyr expand.dtplyr_step 擴展 DataFrame 以包含所有可能的值組合。
- R dtplyr group_by.dtplyr_step 分組和取消分組
- R dtplyr intersect.dtplyr_step 設置操作
- R dtplyr pivot_wider.dtplyr_step 將數據從長軸轉向寬軸
- R dtplyr summarise.dtplyr_step 將每組匯總為一行
- R dtplyr count.dtplyr_step 按組計數觀察值
- R dtplyr select.dtplyr_step 使用名稱對列進行子集化
- R dtplyr drop_na.dtplyr_step 刪除包含缺失值的行
- R dtplyr complete.dtplyr_step 完成缺少數據組合的 DataFrame
- R dtplyr collect.dtplyr_step 強製計算惰性 data.table
- R dtplyr arrange.dtplyr_step 按列值排列行
- R dtplyr separate.dtplyr_step 使用正則表達式或數字位置將字符列分成多列
- R dtplyr rename.dtplyr_step 使用名稱重命名列
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Join data tables。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。