Analogous function for mutate in dplyr.This is a little different from dt_mutate,because it prohibit the in place replacement.

mutate_dt(data, ..., by)

Arguments

data

data.frame

...

List of variables or name-value pairs of summary/modifications functions.

by

unquoted name of grouping variable of list of unquoted names of grouping variables. For details see data.table

Value

data.table

See also

Examples

iris %>% mutate_dt(one = 1)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species one #> 1: 5.1 3.5 1.4 0.2 setosa 1 #> 2: 4.9 3.0 1.4 0.2 setosa 1 #> 3: 4.7 3.2 1.3 0.2 setosa 1 #> 4: 4.6 3.1 1.5 0.2 setosa 1 #> 5: 5.0 3.6 1.4 0.2 setosa 1 #> --- #> 146: 6.7 3.0 5.2 2.3 virginica 1 #> 147: 6.3 2.5 5.0 1.9 virginica 1 #> 148: 6.5 3.0 5.2 2.0 virginica 1 #> 149: 6.2 3.4 5.4 2.3 virginica 1 #> 150: 5.9 3.0 5.1 1.8 virginica 1
mtcars %>% head(3) %>% mutate_dt( cyl2 = cyl * 2, cyl4 = cyl2 * 2 )
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 cyl4 #> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 12 24 #> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 12 24 #> 3: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 8 16
# window functions are useful for grouped mutates iris %>% mutate_dt( rank = rank(-Sepal.Length, ties.method = "min"), keyby = Species)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species rank #> 1: 5.1 3.5 1.4 0.2 setosa 15 #> 2: 4.9 3.0 1.4 0.2 setosa 31 #> 3: 4.7 3.2 1.3 0.2 setosa 40 #> 4: 4.6 3.1 1.5 0.2 setosa 42 #> 5: 5.0 3.6 1.4 0.2 setosa 23 #> --- #> 146: 6.7 3.0 5.2 2.3 virginica 18 #> 147: 6.3 2.5 5.0 1.9 virginica 32 #> 148: 6.5 3.0 5.2 2.0 virginica 23 #> 149: 6.2 3.4 5.4 2.3 virginica 38 #> 150: 5.9 3.0 5.1 1.8 virginica 44