필터 지우기
필터 지우기

Can't get all the rows in my table that have any missing values and remove those rows

조회 수: 5 (최근 30일)
Hi there,
I am trying to remove my table (of a combination of double, cell arrays and duration arrays) of any rows which have missing values. For example, a subset of the table is:
'' '' '441' '0'
'' '' '5' '0'
'No' '' '2889' '0'
'No' 'Clicks' '2894' '0'
'' '' '' '0'
'' '' '5' '0'
'No' 'ROD' '3591' '1'
'' '' '' '0'
I tried this link.
My code is:
badRows = ismissing(finalnbs);
T = finalnbs(~badRows, :);
However, this results in an error:
Row index exceeds table dimensions.
What am I doing wrong, and what code do I need to achieve my need?
  댓글 수: 4
Guillaume
Guillaume 2015년 12월 18일
Also, don't put questions in the title, particularly if it is a different question than in the body.
Both the cyclist and I answer your question in the body (remove any row which has missing value). The answer to remove any row that has more than x missing values is completely different
Dhruv Ghulati
Dhruv Ghulati 2015년 12월 18일
Apologies, you are right, here the question in the title doesn't match the question in the body given. For the sake of other readers I am amending the title.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2015년 12월 18일
This should solve it for you:
A = {
'' '' '441' '0';
'' '' '5' '0';
'No' '' '2889' '0';
'No' 'Clicks' '2894' '0';
'' '' '' '0';
'' '' '5' '0';
'No' 'ROD' '3591' '1';
'' '' '' '0'};
T = cell2table(A)
badEntry = ismissing(T);
badRow = any(badEntry,2)
T = T(~badRow, :);

추가 답변 (1개)

Guillaume
Guillaume 2015년 12월 18일
편집: Guillaume 2015년 12월 18일
badRows is an m*n logical array, where m is the number of rows and n the number of columns of your table. So it has m*n elements. You're then using that as a row index into your table, so of course, matlab can't fit m*n values into m values.
To fix this, use the any (or all) function:
T = finalnbs(~any(badrows, 2), :);
%or
T = finalnbs(all(~badrows, 2), :);
P.S.: this answer your question on how to remove any row which has missing value. If you want to remove rows have more than x missing values, then you use sum instead:
T = finalnbs(sum(badrows, 2) <= x, :);

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by