mx_winnow: Winnow matrix

Description Usage Arguments Details Value Examples

Description

Winnow out NA values from a matrix or data.frame, based on row and column totals.

Usage

1
mx_winnow(x, tol_row = 0, tol_col = 0, order = FALSE)

Arguments

x

matrix or data.frame.

tol_row, tol_col

numeric, giving the number of NAs that will be tolerated in rows or columns. Default 0 will return an array totally free of NA values.

order

logical, should matrix rows and columns be re-ordered by number of NAs upon exiting? Default = FALSE.

Details

The algorithm iteratively removes the most NA-heavy columns and/or rows, up to a user-specified tolerance. Useful, for example, to reduce a huge traits matrix to something that is NA-free or can have just a few NA values to be later imputed or handled.

Value

Modified dataframe or matrix, with added attributes for original and new dimensions, and number of NA that were tolerated in rows and columns.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
### hypothetical data
set.seed(888)
nr <- 10
nc <- 16
n  <- 27  # number of NA to randomly add
x  <- matrix(1, nrow=nr, ncol=nc)
na <- cbind(sample(1:nr,n,replace=TRUE),
            sample(1:nc,n,replace=TRUE))
x[na] <- NA
x  <- data.frame(x)
x
### winnow
(w <- mx_winnow(x, 2, 5)) # tolerate some NA
attributes(w)
(w <- mx_winnow(x, 0, 0)) # tolerate no NA
attributes(w)

phytomosaic/ecole documentation built on Jan. 2, 2022, 11:24 p.m.