這是 dplyr mutate()
泛型的方法。它被轉換為 [.data.table
的 j
參數,使用 :=
修改 "in place"。如果提供了.before
或.after
,則通過調用data.table::setcolorder()
來重新定位新列。
用法
# S3 method for dtplyr_step
mutate(
.data,
...,
.by = NULL,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
參數
- .data
-
一個
lazy_dt()
。 - ...
-
<
data-masking
> Name-value 對。名稱給出輸出中列的名稱。該值可以是:
-
長度為1的向量,將被回收到正確的長度。
-
與當前組(或整個數據幀,如果未分組)長度相同的向量。
-
NULL
,刪除該列。 -
DataFrame 或小標題,用於在輸出中創建多個列。
-
- .by
-
<
tidy-select
> (可選)僅針對此操作選擇要分組的列,作為group_by()
的替代方案。有關詳細信息和示例,請參閱?dplyr_by。 - .keep
-
控製
.data
中的哪些列保留在輸出中。始終保留分組列和...
創建的列。-
"all"
保留.data
中的所有列。這是默認設置。 -
"used"
僅保留...
中使用的列以創建新列。這對於檢查您的工作非常有用,因為它並排顯示輸入和輸出。 -
"unused"
僅保留...
中未使用的列以創建新列。如果您生成新列,但不再需要用於生成它們的列,這非常有用。 -
"none"
不保留.data
中的任何額外列。僅保留...
創建的分組變量和列。
注意:使用 dtplyr
.keep
將僅適用於作為符號傳遞的列名稱,並且不適用於其他工作流程(例如eval(parse(text = "x + 1"))
) -
- .before, .after
-
<
tidy-select
> (可選)控製新列應出現的位置(默認設置是添加到右側)。有關更多詳細信息,請參閱relocate()
。
例子
library(dplyr, warn.conflicts = FALSE)
dt <- lazy_dt(data.frame(x = 1:5, y = 5:1))
dt %>%
mutate(a = (x + y) / 2, b = sqrt(x^2 + y^2))
#> Source: local data table [5 x 4]
#> Call: copy(`_DT24`)[, `:=`(a = (x + y)/2, b = sqrt(x^2 + y^2))]
#>
#> x y a b
#> <int> <int> <dbl> <dbl>
#> 1 1 5 3 5.10
#> 2 2 4 3 4.47
#> 3 3 3 3 4.24
#> 4 4 2 3 4.47
#> 5 5 1 3 5.10
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
# It uses a more sophisticated translation when newly created variables
# are used in the same expression
dt %>%
mutate(x1 = x + 1, x2 = x1 + 1)
#> Source: local data table [5 x 4]
#> Call: copy(`_DT24`)[, `:=`(c("x1", "x2"), {
#> x1 <- x + 1
#> x2 <- x1 + 1
#> .(x1, x2)
#> })]
#>
#> x y x1 x2
#> <int> <int> <dbl> <dbl>
#> 1 1 5 2 3
#> 2 2 4 3 4
#> 3 3 3 4 5
#> 4 4 2 5 6
#> 5 5 1 6 7
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
相關用法
- R dtplyr lazy_dt 創建一個“惰性”data.table 以與 dplyr 動詞一起使用
- R dtplyr group_modify.dtplyr_step 對每個組應用一個函數
- R dtplyr transmute.dtplyr_step 創建新列,刪除舊列
- R dtplyr slice.dtplyr_step 使用行的位置對行進行子集化
- R dtplyr left_join.dtplyr_step 連接數據表
- R dtplyr fill.dtplyr_step 用上一個或下一個值填充缺失值
- R dtplyr filter.dtplyr_step 使用列值對行進行子集化
- R dtplyr distinct.dtplyr_step 子集不同/唯一行
- R dtplyr unite.dtplyr_step 通過將字符串粘貼在一起將多列合並為一列。
- R dtplyr nest.dtplyr_step 巢
- R dtplyr relocate.dtplyr_step 使用變量名稱重新定位變量
- R dtplyr head.dtplyr_step 對第一行或最後一行進行子集化
- R dtplyr expand.dtplyr_step 擴展 DataFrame 以包含所有可能的值組合。
- R dtplyr group_by.dtplyr_step 分組和取消分組
- R dtplyr intersect.dtplyr_step 設置操作
- R dtplyr pivot_wider.dtplyr_step 將數據從長軸轉向寬軸
- R dtplyr summarise.dtplyr_step 將每組匯總為一行
- R dtplyr count.dtplyr_step 按組計數觀察值
- R dtplyr select.dtplyr_step 使用名稱對列進行子集化
- R dtplyr drop_na.dtplyr_step 刪除包含缺失值的行
- R dtplyr complete.dtplyr_step 完成缺少數據組合的 DataFrame
- R dtplyr collect.dtplyr_step 強製計算惰性 data.table
- R dtplyr arrange.dtplyr_step 按列值排列行
- R dtplyr separate.dtplyr_step 使用正則表達式或數字位置將字符列分成多列
- R dtplyr rename.dtplyr_step 使用名稱重命名列
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Create and modify columns。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。