Simple situation.
Using base R
iris_df <- iris
iris_df$Sepal.Length[iris_df$Species == "setosa"] <- NA
Using dplyr
iris_df <- iris %>% mutate(Sepal.Length = if_else(Species == "setosa", NA_real_, Sepal.Length))
I don't mind the extra typing in the tidyr
version if that's what I have to do (I want to embed this in a pipe). But when something takes more typing in tidyr
than base R, I feel I must be missing something? Is there a shorter syntax in tidyr
?
CodePudding user response:
Here's an option using dplyr
that is 6 characters shorter than if_else()
iris %>%
mutate(Sepal.Length = replace(Sepal.Length, Species == "setosa", NA))
Edit:
This one is the exact same length if we remove whitespace:
iris %>%
mutate(Sepal.Length = na_if(Species, "setosa"))
nchar('iris_df$Sepal.Length[iris_df$Species=="setosa"]<-NA')
#> [1] 51
nchar('iris%>%mutate(Sepal.Length=na_if(Species,"setosa"))')
#> [1] 51