Remove rows from table with a column match and an undefined categorical

8 ビュー (過去 30 日間)
Marcus Glover
Marcus Glover 2024 年 2 月 24 日
コメント済み: Voss 2024 年 2 月 24 日
I want to remove all rows from my table T that have a (or multiple) duplicate entries in T.Name and have a value that is undefined in the categorical variable T.Result.
T.Value should be preserved regardless.
%create result array with undefined categoricals
result = [string(missing);"PASS";string(missing);string(missing);"FAIL";"PASS"];
result = categorical(result);
%crreate original table
T=table(["Bill";"Steve";"Joe";"Steve";"Bob";"Steve"],[1;2;3;2;5;2],result,'VariableNames',["Name","Value","Result"]);
T.Result=char(T.Result)
T = 6×3 table
Name Value Result _______ _____ ___________ "Bill" 1 <undefined> "Steve" 2 PASS "Joe" 3 <undefined> "Steve" 2 <undefined> "Bob" 5 FAIL "Steve" 2 PASS
%The solution would eliminate row 4 beacuse "Steve" has a match and T.Result is undefined
%Rows 1 and 3 stay because T.Name does not have a match.
%Row 6 stays because T.Result is defined
solution=T([1:3 5:6],:)
solution = 5×3 table
Name Value Result _______ _____ ___________ "Bill" 1 <undefined> "Steve" 2 PASS "Joe" 3 <undefined> "Bob" 5 FAIL "Steve" 2 PASS

採用された回答

Voss
Voss 2024 年 2 月 24 日
%create result array with undefined categoricals
result = [string(missing);"PASS";string(missing);string(missing);"FAIL";"PASS"];
result = categorical(result);
%crreate original table
T=table(["Bill";"Steve";"Joe";"Steve";"Bob";"Steve"],[1;2;3;2;5;2],result,'VariableNames',["Name","Value","Result"]);
T.Result=char(T.Result)
T = 6×3 table
Name Value Result _______ _____ ___________ "Bill" 1 <undefined> "Steve" 2 PASS "Joe" 3 <undefined> "Steve" 2 <undefined> "Bob" 5 FAIL "Steve" 2 PASS
% logical index by row of whether Name appears more than once in the table:
name_repeated = sum(T.Name.' == T.Name, 2) > 1;
% logical index by row of whether Result is '<undefined>':
result_missing = strcmp(strtrim(cellstr(T.Result)),'<undefined>');
% if you were to keep T.Result as categorical, you could use this expression instead:
% result_missing = ismissing(T.Result);
% rows to remove:
remove_row = name_repeated & result_missing;
% remove the rows:
T(remove_row,:) = []
T = 5×3 table
Name Value Result _______ _____ ___________ "Bill" 1 <undefined> "Steve" 2 PASS "Joe" 3 <undefined> "Bob" 5 FAIL "Steve" 2 PASS
  2 件のコメント
Marcus Glover
Marcus Glover 2024 年 2 月 24 日
Thanks!
Voss
Voss 2024 年 2 月 24 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCategorical Arrays についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by