na.replace | R Documentation |
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))
x |
A matrix with potentially missing values, and also potentially in sparse matrix format (i.e. inherits from "sparseMatrix") |
m |
Optional argument. A vector of values used to replace the missing entries, columnwise. If missing, the column means of 'x' are used |
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.
A version of 'x' is returned with the missing values replaced.
Trevor Hastie
Maintainer: Trevor Hastie hastie@stanford.edu
makeX
and glmnet
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.