Update or add columns when the given condition is met.

mutate_when integrates mutate and case_when in dplyr and make a new tidy verb for data.table. mutate_vars is a super function to do updates in specific columns according to conditions.

mutate_when(.data, when, ..., by)

mutate_vars(.data, .cols = NULL, .func, ..., by)

Arguments

.data

data.frame

when

An object which can be coerced to logical mode

...

Name-value pairs of expressions for mutate_when. Additional parameters to be passed to parameter '.func' in mutate_vars.

by

(Optional) Mutate by what group?

.cols

Any types that can be accepted by select_dt.

.func

Function to be run within each column, should return a value or vectors with same length.

Value

data.table

See also

Examples

iris[3:8,]
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#> 7          4.6         3.4          1.4         0.3  setosa
#> 8          5.0         3.4          1.5         0.2  setosa
iris[3:8,] %>%
  mutate_when(Petal.Width == .2,
              one = 1,Sepal.Length=2)
#> Index: <Petal.Width>
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   one
#>           <num>       <num>        <num>       <num>  <fctr> <num>
#> 1:          2.0         3.2          1.3         0.2  setosa     1
#> 2:          2.0         3.1          1.5         0.2  setosa     1
#> 3:          2.0         3.6          1.4         0.2  setosa     1
#> 4:          5.4         3.9          1.7         0.4  setosa    NA
#> 5:          4.6         3.4          1.4         0.3  setosa    NA
#> 6:          2.0         3.4          1.5         0.2  setosa     1

iris %>% mutate_vars("Pe",scale)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <fctr>
#>   1:          5.1         3.5   -1.3357516  -1.3110521    setosa
#>   2:          4.9         3.0   -1.3357516  -1.3110521    setosa
#>   3:          4.7         3.2   -1.3923993  -1.3110521    setosa
#>   4:          4.6         3.1   -1.2791040  -1.3110521    setosa
#>   5:          5.0         3.6   -1.3357516  -1.3110521    setosa
#>  ---                                                            
#> 146:          6.7         3.0    0.8168591   1.4439941 virginica
#> 147:          6.3         2.5    0.7035638   0.9192234 virginica
#> 148:          6.5         3.0    0.8168591   1.0504160 virginica
#> 149:          6.2         3.4    0.9301544   1.4439941 virginica
#> 150:          5.9         3.0    0.7602115   0.7880307 virginica
iris %>% mutate_vars(is.numeric,scale)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <fctr>
#>   1:  -0.89767388  1.01560199   -1.3357516  -1.3110521    setosa
#>   2:  -1.13920048 -0.13153881   -1.3357516  -1.3110521    setosa
#>   3:  -1.38072709  0.32731751   -1.3923993  -1.3110521    setosa
#>   4:  -1.50149039  0.09788935   -1.2791040  -1.3110521    setosa
#>   5:  -1.01843718  1.24503015   -1.3357516  -1.3110521    setosa
#>  ---                                                            
#> 146:   1.03453895 -0.13153881    0.8168591   1.4439941 virginica
#> 147:   0.55148575 -1.27867961    0.7035638   0.9192234 virginica
#> 148:   0.79301235 -0.13153881    0.8168591   1.0504160 virginica
#> 149:   0.43072244  0.78617383    0.9301544   1.4439941 virginica
#> 150:   0.06843254 -0.13153881    0.7602115   0.7880307 virginica
iris %>% mutate_vars(-is.factor,scale)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <fctr>
#>   1:  -0.89767388  1.01560199   -1.3357516  -1.3110521    setosa
#>   2:  -1.13920048 -0.13153881   -1.3357516  -1.3110521    setosa
#>   3:  -1.38072709  0.32731751   -1.3923993  -1.3110521    setosa
#>   4:  -1.50149039  0.09788935   -1.2791040  -1.3110521    setosa
#>   5:  -1.01843718  1.24503015   -1.3357516  -1.3110521    setosa
#>  ---                                                            
#> 146:   1.03453895 -0.13153881    0.8168591   1.4439941 virginica
#> 147:   0.55148575 -1.27867961    0.7035638   0.9192234 virginica
#> 148:   0.79301235 -0.13153881    0.8168591   1.0504160 virginica
#> 149:   0.43072244  0.78617383    0.9301544   1.4439941 virginica
#> 150:   0.06843254 -0.13153881    0.7602115   0.7880307 virginica
iris %>% mutate_vars(1:2,scale)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <fctr>
#>   1:  -0.89767388  1.01560199          1.4         0.2    setosa
#>   2:  -1.13920048 -0.13153881          1.4         0.2    setosa
#>   3:  -1.38072709  0.32731751          1.3         0.2    setosa
#>   4:  -1.50149039  0.09788935          1.5         0.2    setosa
#>   5:  -1.01843718  1.24503015          1.4         0.2    setosa
#>  ---                                                            
#> 146:   1.03453895 -0.13153881          5.2         2.3 virginica
#> 147:   0.55148575 -1.27867961          5.0         1.9 virginica
#> 148:   0.79301235 -0.13153881          5.2         2.0 virginica
#> 149:   0.43072244  0.78617383          5.4         2.3 virginica
#> 150:   0.06843254 -0.13153881          5.1         1.8 virginica
iris %>% mutate_vars(.func = as.character)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>            <char>      <char>       <char>      <char>    <char>
#>   1:          5.1         3.5          1.4         0.2    setosa
#>   2:          4.9           3          1.4         0.2    setosa
#>   3:          4.7         3.2          1.3         0.2    setosa
#>   4:          4.6         3.1          1.5         0.2    setosa
#>   5:            5         3.6          1.4         0.2    setosa
#>  ---                                                            
#> 146:          6.7           3          5.2         2.3 virginica
#> 147:          6.3         2.5            5         1.9 virginica
#> 148:          6.5           3          5.2           2 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9           3          5.1         1.8 virginica