Description Usage Arguments Author(s) Examples
Secondary function imputes the mean to columns with NA data.
1 | impute.mean(x)
|
x |
a vector to calculate mean |
Jakub Szlek and Aleksander Mendyk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | library(fscaret)
# Make sample matrix
testData <- matrix(data=rep(1:5),ncol=10,nrow=15)
# Replace random values with NA's
n <- 15
replace <- TRUE
set.seed(1)
rand.sample <- sample(length(testData), n, replace=replace)
testData[rand.sample] <- NA
# Print out input matrix
testData
# Record cols with missing values
missing.colsTestMatrix <- which(colSums(is.na(testData))>0)
for(i in 1:length(missing.colsTestMatrix)){
rowToReplace <- missing.colsTestMatrix[i]
testData[,rowToReplace] <- impute.mean(testData[,rowToReplace])
}
# Print out matrix with replaced NA's by column mean
testData
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.