naLast: Move All 'NA' Values to the End of the Rows or Columns of a...

Description Usage Arguments Value Author(s) References Examples

View source: R/naLast.R

Description

Moves all of the NA values in the rows or columns of a matrix to the end of the respective rows or columns.

Usage

1
naLast(inmat, by = "row", outList = FALSE, fill = NA)

Arguments

inmat

The input matrix.

by

Should be either "row" or "col", depending on if you want to shift non-NA values left or up.

outList

Logical. Do you just want a list of the non-NA values? Defaults to FALSE.

fill

While you're at it, do you want to replace NA with some other value?

Value

Either a matrix with the same dimensions as the input matrix or a list with the same number of rows or columns as the input matrix (depending on the choice made in by).

Author(s)

Ananda Mahto

References

http://stackoverflow.com/q/23008142/1270695

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
set.seed(1)
m <- matrix(sample(25, 20, TRUE), ncol = 4,
            dimnames = list(letters[1:5], LETTERS[1:4]))
m[sample(prod(dim(m)), prod(dim(m)) * .6)] <- NA

m

naLast(m, by = "row")
naLast(m, by = "col")
naLast(m, by = "col", outList = TRUE)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.