Skip to content

Instantly share code, notes, and snippets.

@Jfortin1
Last active August 29, 2015 14:10
Show Gist options
  • Save Jfortin1/d4888d68359a36fbda60 to your computer and use it in GitHub Desktop.
Save Jfortin1/d4888d68359a36fbda60 to your computer and use it in GitHub Desktop.
Simple way to impute missing values in matrix
impute.matrix <- function (matrix) {
missing <- which(is.na(matrix) | !is.finite(matrix), arr.ind=TRUE)
if (length(missing)!=0){
for (j in 1:nrow(missing)){
mean <- mean(matrix[missing[j,1],][is.finite(matrix[missing[j,1],])], na.rm=TRUE)
matrix[missing[j,1],missing[j,2]] <- mean
}
}
matrix
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment