I'm trying to understand how to use the across()
function in order to replace _if, replace_at
, variations. But I can't see why this code is not working. I've already checked the "colwise" vignette but it didn't help much.
hmeq <- read.csv("http://www.creditriskanalytics.net/uploads/1/9/5/1/19511601/hmeq.csv",
sep = ",",
header = TRUE)
replace_na_mean <- function(x){
mean <- mean(x, na.rm = TRUE)
x[is.na(x)] <- mean
return(x)
}
hmeq %>% mutate(across(where(is.numeric), replace_na_mean)) #1
hmeq %>% mutate_if(is.numeric, replace_na_mean) %>% head() %>% DT::datatable()#2
#1 Output is basically the original data frame without any modifications, it's as if the code isn't doing anything.
#2 Is working but I'm trying to replace this with mutate()
and across()
:
The
across
is working fine