Integrate mutate
and case_when
in dplyr and make
a new tidy verb for data.table.
mutate_when(data, ...)
data | data.frame |
---|---|
... | the first argument (contents before the first comma) should be the condition, other should be equation(s) just like `mutate_dt`. |
data.table
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 setosairis[3:8,] %>% mutate_when(Petal.Width == .2, one = 1,Sepal.Length=2)#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species one #> 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