Page 1 of 1

Find rows of matrix containing na values

Posted: Sat Mar 24, 2018 11:01 am
by tho_mi
Hey,

I have a matrix in which one column might contain na values. I'd like to get rid of the corresponding rows. What is the easiest way to do that?

Thanks in advance!

Re: Find rows of matrix containing na values

Posted: Mon Mar 26, 2018 9:46 am
by EViews Matt
Hello,

Building on this thread for removing NAs from a vector, the following returns a matrix with the rows containing NAs removed (replacing "m" with the name of your matrix).

Code: Select all

m.@droprow(@emult(@eneq(m * @ones(@rows(m)), m * @ones(@rows(m))), @ranks(@ones(@rows(m)), "a", "i")))

or

Code: Select all

m.@droprow(@emult(@nan(m * @zeros(@rows(m)), 1), @ranks(@ones(@rows(m)), "a", "i")))

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 6:52 am
by tho_mi
Unfortunately this doesn't work.

"@DROPROW is not a member or procedure of M in ..."

Additional question:
What do "@nan" and "@zeros" do? Both are missing in the documentation.

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 7:22 am
by EViews Gareth
Version of EViews?

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 7:54 am
by tho_mi
9.5

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 7:59 am
by EViews Gareth
@droprow is 10 only (there's a reason we ask you to state your version of EViews in every post!).

@zeros creates a matrix of zeros.

@nan changes NAs to some other value.

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 8:14 am
by tho_mi
@droprow:
I assumed that such a basic operation would've been available "earlier".

@zeros:
Ok, so that means the second argument is the number of columns.

@nan:
So I guess the first argument is the "source" matrix/vector and the second argument the value the NAs are changed to?

Since @droprow isn't available that means the question is still open. I assume the easiest way would be to sort the matrix with respect to the column with the NAs and then extracting the submatrix?

Btw, it would be quite helpful if the documentation would have the information on the version number (or at least a note if the procedure or whatever is only available in the newest version).

edit:
I really appreciate your help here (and the fact that you usually reply pretty fast), but the documentation is sometimes annoying because from my perspective important information is missing (like the version number or some procedures, e.g. @nan, which are simply not included).

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 8:44 am
by tho_mi
If the NAs are only present in one column (column 3 here) the following does the job:

Code: Select all

vector m_ranks = @ranks(@columnextract(m, 3), "a", "i")
m = @capplyranks(m, m_ranks)

vector column_with_nas = @columnextract(m, 3)
!current_value = column_with_nas(1)
!i = 1

while @isna(!current_value)
   !i = !i + 1
   !current_value = column_with_nas(!i)
wend

m = @subextract(m, !i, 1)


m is the matrix to be "cleaned". It has to be sorted ascending first, so that the NAs are at the beginning.

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 8:46 am
by EViews Gareth

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 9:22 am
by EViews Matt
Here's a more general, EViews 9.5-friendly solution that (1) doesn't require all the NAs to be in the same column, and (2) preserves the order of the non-NA-containing rows. This solution constructs a new matrix as the result rather than modifying the original matrix.

Code: Select all

matrix(1,m.@cols) m2
for !i = 1 to @rows(m)
   if not @isna(@inner(@rowextract(m, !i))) then
      m2 = @vcat(m2, @transpose(@rowextract(m, !i)))
   endif
next
m2 = @subextract(m2, 2, 1)

Re: Find rows of matrix containing na values

Posted: Tue Mar 27, 2018 9:29 am
by tho_mi
Thanks! In my case the matrix has to be sorted anyway (and the NAs are only in one column), so it doesn't have to be that general (but I'm sure it's helpful for other people) :)