Effacer les filtres
Effacer les filtres

Exclude rows from table if multiple conditions are met

9 vues (au cours des 30 derniers jours)
YaaW
YaaW le 21 Juil 2022
Commenté : YaaW le 22 Juil 2022
Hi,
I have a table with many columns and rows (long list of patients that have had many questionnaires). I want to exclude patients that haven't filled in any of 5 questionnaires, but want to keep them if they have filled in all or for example only 1 of them. I tried it with an if loop, but I get the error 'Conversion to logical form from table is not possible.'
For example for the following table
A B C D E
1 NaN NaN NaN 4 NaN
2 NaN NaN NaN NaN NaN
3 8 6 9 3 5
4 4 8 NaN 5 4
5 NaN NaN NaN NaN NaN
6 NaN 3 2 NaN NaN
I want to exclude rows 2 and 5 since all questionnaires are not filled in but keep the rest, including the rows with only a few filled-in questionnaires.
I tried it with an if loop (if all 5 questionnaires are not NaN, then keep the rows, otherwise delete them), but then got the error as written above.
Anyone know how to do this? Thanks!
  1 commentaire
Matt J
Matt J le 21 Juil 2022
I have a table with many columns
Is it really a Matlab table data type, as in,

Connectez-vous pour commenter.

Réponse acceptée

Kevin Holly
Kevin Holly le 21 Juil 2022
m = [1 NaN NaN NaN 4 NaN
2 NaN NaN NaN NaN NaN
3 8 6 9 3 5
4 4 8 NaN 5 4
5 NaN NaN NaN NaN NaN
6 NaN 3 2 NaN NaN]
m = 6×6
1 NaN NaN NaN 4 NaN 2 NaN NaN NaN NaN NaN 3 8 6 9 3 5 4 4 8 NaN 5 4 5 NaN NaN NaN NaN NaN 6 NaN 3 2 NaN NaN
rmmissing(m,'MinNumMissing',5)
ans = 4×6
1 NaN NaN NaN 4 NaN 3 8 6 9 3 5 4 4 8 NaN 5 4 6 NaN 3 2 NaN NaN
  4 commentaires
Kevin Holly
Kevin Holly le 21 Juil 2022
Yes, please see below
t = [NaN NaN NaN 4 NaN
NaN NaN NaN NaN NaN
8 6 9 3 5
4 8 NaN 5 4
NaN NaN NaN NaN NaN
NaN 3 2 NaN NaN];
t=array2table(t)
t = 6×5 table
t1 t2 t3 t4 t5 ___ ___ ___ ___ ___ NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN 8 6 9 3 5 4 8 NaN 5 4 NaN NaN NaN NaN NaN NaN 3 2 NaN NaN
t.Properties.VariableNames = {'A' 'B' 'C' 'D' 'E'};
new_t = rmmissing(t,'MinNumMissing',3,'DataVariables',["A" "B" "C"])
new_t = 3×5 table
A B C D E ___ _ ___ ___ ___ 8 6 9 3 5 4 8 NaN 5 4 NaN 3 2 NaN NaN
YaaW
YaaW le 22 Juil 2022
Thank you very much!! It worked!

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 21 Juil 2022
A=randi(5,5); A(A<=4)=nan
A = 5×5
NaN NaN NaN NaN NaN 5 NaN NaN NaN 5 NaN NaN NaN NaN NaN NaN 5 NaN NaN 5 NaN NaN NaN 5 NaN
A(all(isnan(A),2),:)=[]
A = 3×5
5 NaN NaN NaN 5 NaN 5 NaN NaN 5 NaN NaN NaN 5 NaN

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by