Another way to do it using sapply, but using replace instead of ifelse:
df <- data.frame(A = c(1, 2, NA, 4),
B = c(5, NA, 3, 1),
C = c(NA, 2, 3, NA))
(df <- sapply(X = df,
FUN = function(t)
replace(x = t,
list = is.na(x = t),
values = mean(x = t,
na.rm = TRUE))))
#> A B C
#> [1,] 1.000000 5 2.5
#> [2,] 2.000000 3 2.0
#> [3,] 2.333333 3 3.0
#> [4,] 4.000000 1 2.5