moved numeric cast after check for NA/NaN/Inf/NULL to avoid NA coercion