這是tidyr::expand 泛型的方法。它不會顯式對結果進行排序,因此順序可能與 expand()
針對數據幀返回的順序不同。
參數
- data
-
由數據庫查詢支持的惰性 DataFrame 。
- ...
-
要擴展的列規範。有關更多詳細信息,請參閱tidyr::expand。
- .name_repair
-
有問題的列名的處理:
-
"minimal"
:沒有名稱修複或檢查,超出基本存在, -
"unique"
:確保名稱唯一且不為空, -
"check_unique"
:(默認值),沒有名稱修複,但檢查它們是unique
, -
"universal"
:命名為unique
和語法 -
函數:應用自定義名稱修複(例如,
.name_repair = make.names
用於基本 R 樣式的名稱)。 -
purrr-style 匿名函數,請參閱
rlang::as_function()
此參數作為
repair
傳遞到vctrs::vec_as_names()
。有關這些條款以及用於執行這些條款的策略的更多詳細信息,請參閱此處。 -
值
另一個tbl_lazy
。使用show_query()
查看生成的查詢,並使用collect()
執行查詢並將數據返回到R。
例子
fruits <- memdb_frame(
type = c("apple", "orange", "apple", "orange", "orange", "orange"),
year = c(2010, 2010, 2012, 2010, 2010, 2012),
size = c("XS", "S", "M", "S", "S", "M"),
weights = rnorm(6)
)
# All possible combinations ---------------------------------------
fruits %>% tidyr::expand(type)
#> # Source: SQL [2 x 1]
#> # Database: sqlite 3.41.2 [:memory:]
#> type
#> <chr>
#> 1 apple
#> 2 orange
fruits %>% tidyr::expand(type, size)
#> # Source: SQL [6 x 2]
#> # Database: sqlite 3.41.2 [:memory:]
#> type size
#> <chr> <chr>
#> 1 apple XS
#> 2 apple S
#> 3 apple M
#> 4 orange XS
#> 5 orange S
#> 6 orange M
# Only combinations that already appear in the data ---------------
fruits %>% tidyr::expand(nesting(type, size))
#> # Source: SQL [4 x 2]
#> # Database: sqlite 3.41.2 [:memory:]
#> type size
#> <chr> <chr>
#> 1 apple XS
#> 2 orange S
#> 3 apple M
#> 4 orange M
相關用法
- R dbplyr escape 轉義/引用字符串。
- R dbplyr backend-teradata 後端:Teradata
- R dbplyr distinct.tbl_lazy 子集不同/唯一行
- R dbplyr backend-sqlite 後端:SQLite
- R dbplyr pivot_wider.tbl_lazy 將數據從長軸轉向寬軸
- R dbplyr build_sql 構建 SQL 字符串。
- R dbplyr mutate.tbl_lazy 創建、修改和刪除列
- R dbplyr collapse.tbl_sql 計算查詢的結果
- R dbplyr sql_expr 從 R 表達式生成 SQL
- R dbplyr get_returned_rows 提取並檢查返回的行
- R dbplyr dbplyr_uncount “計數”數據庫表
- R dbplyr count.tbl_lazy 按組計數觀察值
- R dbplyr backend-odbc 後端:ODBC
- R dbplyr head.tbl_lazy 對第一行進行子集化
- R dbplyr db-quote SQL 轉義/引用泛型
- R dbplyr copy_inline 在 dbplyr 查詢中使用本地 DataFrame
- R dbplyr backend-oracle 後端:甲骨文
- R dbplyr backend-snowflake 後端:雪花
- R dbplyr lahman 緩存並檢索 Lahman 棒球數據庫的 src_sqlite。
- R dbplyr backend-redshift 後端:紅移
- R dbplyr partial_eval 部分評估表達式。
- R dbplyr group_by.tbl_lazy 按一個或多個變量分組
- R dbplyr tbl_lazy 創建本地惰性 tibble
- R dbplyr backend-hana 後端:SAP HANA
- R dbplyr translate_sql 將表達式轉換為 SQL
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Expand SQL tables to include all possible combinations of values。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。