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


R dtplyr relocate.dtplyr_step 使用變量名稱重新定位變量

這是 dplyr relocate() 泛型的方法。它被轉換為 [.data.tablej 參數。

用法

# S3 method for dtplyr_step
relocate(.data, ..., .before = NULL, .after = NULL)

參數

.data

一個lazy_dt()

...

<tidy-select > 要移動的列。

.before, .after

< tidy-select > ... 選擇的列的目標。兩者都不提供會將列移動到左側;指定兩者都是錯誤的。

例子

library(dplyr, warn.conflicts = FALSE)

dt <- lazy_dt(data.frame(x = 1, y = 2, z = 3))

dt %>% relocate(z)
#> Source: local data table [1 x 3]
#> Call:   setcolorder(copy(`_DT33`), c("z", "x", "y"))
#> 
#>       z     x     y
#>   <dbl> <dbl> <dbl>
#> 1     3     1     2
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt %>% relocate(y, .before = x)
#> Source: local data table [1 x 3]
#> Call:   setcolorder(copy(`_DT33`), c("y", "x", "z"))
#> 
#>       y     x     z
#>   <dbl> <dbl> <dbl>
#> 1     2     1     3
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt %>% relocate(y, .after = y)
#> Source: local data table [1 x 3]
#> Call:   `_DT33`
#> 
#>       x     y     z
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Relocate variables using their names。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。