
Last chance! 50% off unlimited learning
Sale ends in
Missing entries in any given column of the matrix are replaced by the column means or the values in a supplied vector.
na.replace(x, m = rowSums(x, na.rm = TRUE))
A matrix with potentially missing values, and also potentially in sparse matrix format (i.e. inherits from "sparseMatrix")
Optional argument. A vector of values used to replace the missing entries, columnwise. If missing, the column means of 'x' are used
A version of 'x' is returned with the missing values replaced.
This is a simple imputation scheme. This function is called by makeX
if the na.impute=TRUE
option is used, but of course can be used on
its own. If 'x' is sparse, the result is sparse, and the replacements are
done so as to maintain sparsity.
makeX
and glmnet
# NOT RUN {
set.seed(101)
### Single data frame
X = matrix(rnorm(20), 10, 2)
X[3, 1] = NA
X[5, 2] = NA
X3 = sample(letters[1:3], 10, replace = TRUE)
X3[6] = NA
X4 = sample(LETTERS[1:3], 10, replace = TRUE)
X4[9] = NA
dfn = data.frame(X, X3, X4)
x = makeX(dfn)
m = rowSums(x, na.rm = TRUE)
na.replace(x, m)
x = makeX(dfn, sparse = TRUE)
na.replace(x, m)
# }
Run the code above in your browser using DataLab