Replace the missing entries in a matrix columnwise with the entries in a supplied vector
Replace the missing entries in a matrix columnwise with the entries in a supplied vector
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))
Arguments
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
Returns
A version of 'x' is returned with the missing values replaced.
Details
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.
Examples
set.seed(101)### Single data frameX = matrix(rnorm(20),10,2)X[3,1]=NAX[5,2]=NAX3 = sample(letters[1:3],10, replace =TRUE)X3[6]=NAX4 = sample(LETTERS[1:3],10, replace =TRUE)X4[9]=NAdfn = 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)