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


R dtplyr rename.dtplyr_step 使用名稱重命名列

這些是 dplyr 泛型 rename()rename_with() 的方法。它們都被翻譯為 data.table::setnames()

用法

# S3 method for dtplyr_step
rename(.data, ...)

# S3 method for dtplyr_step
rename_with(.data, .fn, .cols = everything(), ...)

參數

.data

lazy_dt()

...

對於 rename() : < tidy-select > 使用 new_name = old_name 重命名選定的變量。

對於 rename_with() :傳遞給 .fn 的附加參數。

.fn

用於轉換所選 .cols 的函數。應返回與輸入長度相同的字符向量。

.cols

<tidy-select> 要重命名的列;默認為所有列。

例子

library(dplyr, warn.conflicts = FALSE)
dt <- lazy_dt(data.frame(x = 1, y = 2, z = 3))
dt %>% rename(new_x = x, new_y = y)
#> Source: local data table [1 x 3]
#> Call:   setnames(copy(`_DT34`), c("x", "y"), c("new_x", "new_y"))
#> 
#>   new_x new_y     z
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt %>% rename_with(toupper)
#> Source: local data table [1 x 3]
#> Call:   setnames(copy(`_DT34`), toupper)
#> 
#>       X     Y     Z
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
源代碼:R/step-call.R

相關用法


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