Get positions for NAs only in the "middle" of a matrix column
- by Abiel
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.