MATLAB: Get means for nested categorical variables

grouping

Assuming you have the following table:
site = {'1','1','1','1','2','2','2','2','3','3','3','3'}';
site = categorical(site);
days = {'0','0','45','45','0','0','36','36','0','0','67','67'}';
days = categorical(days);
var = [23,45,67,21,42,87,84,65,12,77,42,90]';
T = table(site,days,var);
how do I make a new table T1 with each unique value of site and days, and the means of var over the days category? e.g. T1 = table(site,days,meanvar)

Best Answer

Please try the following:
[group,site,days] = findgroups(T.site,T.days);
meanvar = splitapply(@mean,T.var,group);
T1 = table(site,days,meanvar);