0

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.

Output without any changes to original df

#2 Is working but I'm trying to replace this with mutate() and across():

Output I want but using code #1

user2554330
  • 37,248
  • 4
  • 43
  • 90
igna43
  • 11
  • 3
  • It is working fine `hmeq %>% mutate(across(where(is.numeric), replace_na_mean)) %>% filter(BAD == 1, LOAN == 1500) %>% pull(MORTDUE)# [1] 13500.00 73760.82`. Have you assigned the output back to the object – akrun Oct 02 '20 at 02:48
  • One suggestion is not to use assign objects with the function name i.e. `mean` – akrun Oct 02 '20 at 02:58
  • Thank you for the answer, but I still have the same issue, I'm about to pull the hair off my head. It should work, but for some reason it's not doing anything. – igna43 Oct 02 '20 at 03:17
  • Can you check your package version – akrun Oct 02 '20 at 03:21
  • dplyr version 1.0.2. Maybe I should be using 1.0.1? – igna43 Oct 02 '20 at 03:25
  • I tested with `1.0.1`. That would be the difference. Can you change the function to `replace_na_mean <- function(x) replace(x, is.na(x), mean(x, na.rm = TRUE))` – akrun Oct 02 '20 at 03:25
  • I just tried that function, it's working fine, but the across function apparently isn't recognising it as it should. Same with the previous function definition. – igna43 Oct 02 '20 at 03:30
  • May be there is a bug in the new version. – akrun Oct 02 '20 at 03:31

1 Answers1

0

The across is working fine

library(dplyr) # version - 1.0.1
out <- hmeq %>%
       mutate(across(where(is.numeric), replace_na_mean)) 

out %>%
     filter(BAD == 1, LOAN == 1500) %>% 
     pull(MORTDUE)
  #[1] 13500.00 73760.82
akrun
  • 874,273
  • 37
  • 540
  • 662