In base R I can use the following code to remove/replace values throughout a dataframe (e.g., all NAs or values greater than 0.99).
df[df > 0.99] <- NA
df[is.na(df)] <- 0L
Is there a way to perform the equivalent operation using dplyr in the tidyverse?
You can use mutate_all
with replace
:
df = data.frame(x = c(1.2, 0.4, NA, 0.6), y = c(NA, 0.3, 0.992, 0.5))
df %>% mutate_all(~ replace(., . > 0.99 | is.na(.), 0))
# x y
#1 0.0 0.0
#2 0.4 0.3
#3 0.0 0.0
#4 0.6 0.5
Or use funs
:
df %>% mutate_all(funs(replace(., . > 0.99 | is.na(.), 0)))
If you just need to integrate this as part of the pipe, replace
method should work:
df %>% replace(. > 0.99 | is.na(.), 0)
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