I found a way to get it working with replace_na
as requested (as it is the fastest option via microbenchmark testing):
UPDATE with dplyr v1.0.0
This has been made much easier with addition of the dplyr::across
function:
library(dplyr)
library(tidyr)
mtcars %>%
mutate(
across(everything(), ~replace_na(.x, 0))
)
# Or if you're pipe shy:
mutate(mtcars, across(everything(), ~replace_na(.x, 0)))
That's it! Pretty simple stuff.
For dplyr < v1.0.0
library(tidyr)
library(dplyr)
# First, create a list of all column names and set to 0
myList <- setNames(lapply(vector("list", ncol(mtcars)), function(x) x <- 0), names(mtcars))
# Now use that list in tidyr::replace_na
mtcars %>% replace_na(myList)
To apply this to your working data frame, be sure to replace the 2 instances of mtcars
with whatever you have named your working data frame when creating the myList
object.