當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R dbplyr expand.tbl_lazy 擴展 SQL 表以包含所有可能的值組合

這是tidyr::expand 泛型的方法。它不會顯式對結果進行排序,因此順序可能與 expand() 針對數據幀返回的順序不同。

用法

# S3 method for tbl_lazy
expand(data, ..., .name_repair = "check_unique")

參數

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/verb-expand.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Expand SQL tables to include all possible combinations of values。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。