Last active
August 29, 2015 14:10
-
-
Save Jfortin1/d4888d68359a36fbda60 to your computer and use it in GitHub Desktop.
Simple way to impute missing values in matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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