Get positions for NAs only in the "middle" of a matrix column
Posted
by
Abiel
on Stack Overflow
See other posts from Stack Overflow
or by Abiel
Published on 2011-01-28T21:38:35Z
Indexed on
2011/01/28
23:26 UTC
Read the original article
Hit count: 262
r
I want to obtain an index that refers to the positions of NA values in a matrix where the index is true if a given cell is NA and there is at least one non-NA value before and after it in the column. For example, given the following matrix
[,1] [,2] [,3] [,4]
[1,] NA 1 NA 1
[2,] 1 NA NA 2
[3,] NA 2 NA 3
the only value of the index that comes back TRUE should be [2,2].
Is there a compact expression for what I want to do? If I had to I could loop through columns and use something like min(which(!is.na(x[,i])))
to find the first non-NA value in each column, and then set all values before that to FALSE (and the same for all values after the max). This way I would not select leading and trailing NA values. But this seems a bit messy, so I'm wondering if there is a cleaner expression that does this without loops.
© Stack Overflow or respective owner