Solution :
I guess you are failing to replace Inf values with the means of column vectors. This is happening because of the na.rm = TRUE argument in your call to mean() within the impute.mean function which does exactly what it says i.e. it removes NA values and clearly not Inf ones
You can cross check it, by following code example:
impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l), mean(l, na.rm = TRUE))
losses <- apply(losses, 2, impute.mean)
sum( apply( losses, 2, function(.) sum(is.infinite(.))) )
# [1] 696
To remove the infinite values, please use following:
impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l), mean(l[!is.na(l) & !is.nan(l) & !is.infinite(l)]))
losses <- apply(losses, 2, impute.mean)
sum(apply( losses, 2, function(.) sum(is.infinite(.)) ))
# [1] 0