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


R rsample rset_reconstruct 使用新的 rset 子類擴展 rsample

rset_reconstruct() 封裝了允許新的 rset 子類與 vctrs (通過 vctrs::vec_restore() )和 dplyr (通過 dplyr::dplyr_reconstruct() )正常工作的邏輯。它旨在成為一個開發人員工具,正常使用 rsample 不需要它。

用法

rset_reconstruct(x, to)

參數

x

用於恢複到 rset 子類的數據幀。

to

要恢複到的 rset 子類。

x 恢複為 to 的 rset 子類。

細節

在 vctrs/dplyr 操作之後,如果滿足以下條件,則將 rset 對象視為"reconstructable":

  • xto 都有一個名為 "splits" 的相同列(列和行順序無關緊要)。

  • xto 都有相同的列,前綴為 "id"(列和行順序無關緊要)。

例子

to <- bootstraps(mtcars, times = 25)

# Imitate a vctrs/dplyr operation,
# where the class might be lost along the way
x <- tibble::as_tibble(to)

# Say we added a new column to `x`. Here we mock a `mutate()`.
x$foo <- "bar"

# This is still reconstructable to `to`
rset_reconstruct(x, to)
#> # Bootstrap sampling 
#> # A tibble: 25 × 3
#>    splits          id          foo  
#>    <list>          <chr>       <chr>
#>  1 <split [32/12]> Bootstrap01 bar  
#>  2 <split [32/9]>  Bootstrap02 bar  
#>  3 <split [32/13]> Bootstrap03 bar  
#>  4 <split [32/12]> Bootstrap04 bar  
#>  5 <split [32/12]> Bootstrap05 bar  
#>  6 <split [32/13]> Bootstrap06 bar  
#>  7 <split [32/11]> Bootstrap07 bar  
#>  8 <split [32/12]> Bootstrap08 bar  
#>  9 <split [32/14]> Bootstrap09 bar  
#> 10 <split [32/12]> Bootstrap10 bar  
#> # ℹ 15 more rows

# Say we lose the first row
x <- x[-1, ]

# This is no longer reconstructable to `to`, as `x` is no longer an rset
# bootstraps object with 25 bootstraps if one is lost!
rset_reconstruct(x, to)
#> # A tibble: 24 × 3
#>    splits          id          foo  
#>    <list>          <chr>       <chr>
#>  1 <split [32/9]>  Bootstrap02 bar  
#>  2 <split [32/13]> Bootstrap03 bar  
#>  3 <split [32/12]> Bootstrap04 bar  
#>  4 <split [32/12]> Bootstrap05 bar  
#>  5 <split [32/13]> Bootstrap06 bar  
#>  6 <split [32/11]> Bootstrap07 bar  
#>  7 <split [32/12]> Bootstrap08 bar  
#>  8 <split [32/14]> Bootstrap09 bar  
#>  9 <split [32/12]> Bootstrap10 bar  
#> 10 <split [32/10]> Bootstrap11 bar  
#> # ℹ 14 more rows

相關用法


注:本文由純淨天空篩選整理自Hannah Frick等大神的英文原創作品 Extending rsample with new rset subclasses。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。