Example:
mydf <- data.frame(x = 1:10, y = rnorm(10))
mydf[2,2] <- NA
mydf[5,1] <- NA
Looks like:
mydf
x y
1 1 -1.0198834
2 2 NA
3 3 0.6080986
4 4 -0.4124830
5 NA 0.1723944
6 6 -2.3869210
7 7 -0.9843615
8 8 -1.3187416
9 9 0.1794670
10 10 0.9708277
I want to replace all NAs in the entire df with 0. I read this blog post then tried:
mydf |> replace(is.na(_), '') # Error in replace(mydf, is.na("_"), "") : invalid use of pipe placeholder
mydf |> mutate(across(everything(), .fns = ~replace_na(_, 0))) # Error in mydf |> mutate(across(everything(), .fns = ~replace_na("_", 0))) : invalid use of pipe placeholder
mydf |> mutate(across(everything(), .fns = ~replace_na(data = _, 0))) # same error, tried passing placeholder to a named arg per the blog post "One thing that is different than the . placeholder is the fact that you need to pass the name of the argument that will receive the _ placeholder"
How can I replace all NA with 0 in my df within my dplyr chain in this way?