Moves all of the NA
values in the rows or columns of a matrix to the
end of the respective rows or columns.
naLast(inmat, by = "row", outList = FALSE, fill = NA)
inmat | The input matrix. |
---|---|
by | Should be either |
outList | Logical. Do you just want a |
fill | While you're at it, do you want to replace |
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
).
http://stackoverflow.com/q/23008142/1270695
Ananda Mahto
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#> A B C D #> a NA 23 1 14 #> b NA 11 NA NA #> c NA 14 21 7 #> d NA NA NA NA #> e NA NA 22 NAnaLast(m, by = "row")#> A B C D #> a 23 1 14 NA #> b 11 NA NA NA #> c 14 21 7 NA #> d NA NA NA NA #> e 22 NA NA NAnaLast(m, by = "col")#> A B C D #> a NA 23 1 14 #> b NA 11 21 7 #> c NA 14 22 NA #> d NA NA NA NA #> e NA NA NA NAnaLast(m, by = "col", outList = TRUE)#> [[1]] #> named integer(0) #> #> [[2]] #> a b c #> 23 11 14 #> #> [[3]] #> a c e #> 1 21 22 #> #> [[4]] #> a c #> 14 7 #>