How do I pull out from a table, the total mean of one item based on a particular condition (for several subjects), and then compute the difference in mean values?
5 次查看(过去 30 天)
显示 更早的评论
I have a large table of data (example below).
For each "Subject", I need to determine 1) the total mean value of all "Item As" that are 'High' in "Condition Z", and 2) the total mean value of all "Item As" that are 'Low' in "Condition Z". For example, Subject 1 the answers would be: 25 (i.e. The mean for Item A for 'High' "Condition Z" items); and 50 (i.e. The mean for Item A for 'Low' "Condition Z" items)
I then need to generate a table that, for each "Subject", lists the difference in these two values as follows: ("total mean value of all "Item As" that are 'High' in "Condition Z"") - ("the total mean value of all "Item As" that are 'Low' in "Condition Z"). Thank you very much!
Subject Item A Condition Z Restudied Class
1 30 High Y A
1 20 High N B
1 45 Low N A
1 55 Low Y B
2 17 High Y A
2 45 Low N B
2 13 High Y A
3 56 High Y A
3 12 Low N B
3 34 Low N B
3 15 High N A
0 个评论
采纳的回答
Dave B
2021-10-22
编辑:Dave B
2021-10-22
You can do the first part with groupsummary
Subject = [1 1 1 1 2 2 2 3 3 3 3]';
ItemA=[30 20 45 55 17 45 13 56 12 34 15]';
ConditionZ=["High" "High" "Low" "Low" "High" "Low" "High" "High" "Low" "Low" "High"]';
t=table(Subject,ItemA,ConditionZ)
tsummary=groupsummary(t,["Subject" "ConditionZ"],"mean","ItemA")
You might be tempted to do the second part by just subtracting tsummary.mean_ItemA(tsummary.ConditionZ=="High") - tsummary.mean_ItemA(tsummary.ConditionZ=="low")...but i think a nicer solution is to make two separate tables and join them which will make sure that all of the indices line up.
thigh = tsummary(tsummary.ConditionZ=="High",:)
tlow = tsummary(tsummary.ConditionZ=="Low",:)
tdiff=join(tlow,thigh,'Keys','Subject')
tdiff.difference = tdiff.mean_ItemA_thigh - tdiff.mean_ItemA_tlow
tdiff=tdiff(:,[1 end]) % just for display, no reason to eliminate the intermediate columns
4 个评论
dpb
2021-10-23
Yeah, this (relatively) recent penchant to introduce so many (almost) overlapping functionalities in common toolboxes or even in base product is maddening in causing so much overhead and bloat in the dictionary space; much of which then ends up being deprecated but never going away. The early incarnation of the table as the Statistics TB dataset and its implementation of a different categorical class is a prime example.
While such were indeed needed, it would have been far better for the TB and the base product to have been integrated together even if it meant delaying some TB functionality for a while.
The seeming lack of an overall comprehensive design and direction is becoming a real issue in my mind; the disparate user interfaces and inconsistencies continue to multiply.
$0.02, imo, ymmv, etc., etc., ...
更多回答(1 个)
dpb
2021-10-22
tMEANS=rowfun(@mean,tData,'GroupingVariables'{'ConditionZ'},'InputVariables','ItemA','OutputVariableNames','GroupedMeans');
See the doc for all the skinny on grouping variables and rowfun and friends...you might also find groupsummary of interest.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!