How do you split a table into sub-tables based on entries in a specific column?
150 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2020 年 5 月 27 日
編集済み: MathWorks Support Team
2022 年 10 月 13 日
How do you split a table into sub-tables based on entries in a specific column? Essentially, I want to divide my table data into groups, determined by one of its values.
採用された回答
MathWorks Support Team
2022 年 10 月 13 日
編集済み: MathWorks Support Team
2022 年 10 月 13 日
I have created a simple example which demonstrates how to split a table based on the first column as the category variable.
The script is also attached as "test_script.m". Please read the comments to get a better understanding of how to use the script for your use case.
% Patient pateint data
load patients
% Create test table
T = table(Gender(1:5), Height(1:5), 'VariableNames', {'Gender', 'Height'});
% Group based on first column - gender column
% Modify 1 to appropriate column number
G = findgroups(T{:, 1});
% Split table based on first column - gender column
T_split = splitapply( @(varargin) varargin, T , G);
% Allocate empty cell array fo sizxe equal to number of rows in T_Split
subTables = cell(size(T_split, 1));
% Create sub tables
for i = 1:size(T_split, 1)
subTables{i} = table(T_split{i, :}, 'VariableNames', ...
T.Properties.VariableNames);
end
% Display Results
disp('Full Table:');
disp(T);
disp('Sub Table 1:');
disp(subTables{1});
disp('Sub Table 2:');
disp(subTables{2});
See this documentation for another example of using 'splitapply':
Here is a list of the functions I am using:
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!