Get means for nested categorical variables
조회 수: 1 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
채택된 답변
Akira Agata
2018년 3월 14일
Please try the following:
[group,site,days] = findgroups(T.site,T.days);
meanvar = splitapply(@mean,T.var,group);
T1 = table(site,days,meanvar);
추가 답변 (2개)
Peter Perkins
2018년 3월 15일
Another possibility:
>> T2 = varfun(@mean,T,'GroupingVariables',{'site' 'days'})
T2 =
6×4 table
site days GroupCount mean_var
____ ____ __________ ________
1 0 2 34
1 45 2 44
2 0 2 64.5
2 36 2 74.5
3 0 2 44.5
3 67 2 66
By the way, "1", "2", etc. are perhaps perfectly good category names for the site variable (as long as you remember that the values are text, and not the numeric values 1, 2, etc), but it seems like days is a numeric count. If that's true, there's no particular reason why you can't just leave it as numeric. The above code, and indeed the version usig findgroups/splitapply, continues to work.
댓글 수: 0
Razvan Carbunescu
2018년 3월 16일
>> GT = groupsummary(T,{'site','days'},'mean','var')
GT =
6×4 table
site days GroupCount mean_var
____ ____ __________ ________
1 0 2 34
1 45 2 44
2 0 2 64.5
2 36 2 74.5
3 0 2 44.5
3 67 2 66
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!