What would be the dplyr analog of performing the following operation in base R?
iris$Sepal.Length[iris$Sepal.Length>2] <- iris$Sepal.Length[iris$Sepal.Length>2] * 10
I am trying to use filter but cannot go back to the original data set (without a join
)
You can use mutate
with ifelse
to get the same results as comments from @alistaire:
iris %>% mutate(Sepal.Length = ifelse(Sepal.Length > 2, Sepal.Length * 10, Sepal.Length))
If we are using data.table
, we can avoid the ifelse
and make it faster
library(data.table)
as.data.table(iris)[Sepal.Length > 2, Sepal.Length := Sepal.Length * 10]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With