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!
Find rows of matrix containing na values
Moderators: EViews Gareth, EViews Jason, EViews Moderator

 EViews Developer
 Posts: 262
 Joined: Thu Apr 25, 2013 7:48 pm
Re: Find rows of matrix containing na values
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).
or
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
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.
"@DROPROW is not a member or procedure of M in ..."
Additional question:
What do "@nan" and "@zeros" do? Both are missing in the documentation.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11813
 Joined: Tue Sep 16, 2008 5:38 pm

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11813
 Joined: Tue Sep 16, 2008 5:38 pm
Re: Find rows of matrix containing na values
@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.
@zeros creates a matrix of zeros.
@nan changes NAs to some other value.
Follow us on Twitter @IHSEViews
Re: Find rows of matrix containing na values
@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).
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
If the NAs are only present in one column (column 3 here) the following does the job:
m is the matrix to be "cleaned". It has to be sorted ascending first, so that the NAs are at the beginning.
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.

 Fe ddaethom, fe welon, fe amcangyfrifon
 Posts: 11813
 Joined: Tue Sep 16, 2008 5:38 pm

 EViews Developer
 Posts: 262
 Joined: Thu Apr 25, 2013 7:48 pm
Re: Find rows of matrix containing na values
Here's a more general, EViews 9.5friendly solution that (1) doesn't require all the NAs to be in the same column, and (2) preserves the order of the nonNAcontaining 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
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)
Who is online
Users browsing this forum: No registered users and 10 guests