One approach to solve this issue is to use categorical variables, which will allow you to identify the frequencies of each entries and then logical index to create the subset tables. This workflow is demonstrated in the code below:
T = convertvars(T, "Var1", 'categorical');
counts = countcats(T.Var1);
cats = categories(T.Var1);
uniqueCounts = unique(counts);
splitTables = cell(length(uniqueCounts));
for i = 1:length(uniqueCounts)
currentCountCats = cats(counts == i);
splitTables{i} = T(ismember(T.Var1, currentCountCats), :);
end
This results in the following tables:
>> splitTables{1,1}
ans =
2×2 table
Var1 Var2
____ _______
A "data1"
B "data2"
>> splitTables{2,1}
ans =
2×2 table
Var1 Var2
____ _______
C "data3"
C "data4"
>> splitTables{3,1}
ans =
3×2 table
Var1 Var2
____ _______
D "data5"
D "data6"
D "data7"
First, you can make a variable categorical when creating the initial table by using 'categorical' or, for an existing table, you can convert the variable to categorical using 'convertvars'. Documentation for each function can be found at the links below:
Once the entries are in a categorical variable, you can use 'categories' to get a list of the categories and 'countcats' to obtain the frequency of each category. Please see the following links for more information on these functions:
Finally, you can use logical indexing to find the categories of a certain frequency and pull the relevant rows from the original table using 'ismember'. More information on logical indexing and 'ismember' can be found here:
Note that the same process can be followed for numeric variables as well.
Best Answer