MATLAB: Percentage in tablerows with subgrouping

percentagetablerows

I have the following table as shown below; i want to get % of true vs false for each subjID, condition combination; example first row 1,1 false should be 20% and second row should be 80% and so on thanks
subjID condition correctAnswer GroupCount sum_correctAnswer
______ _________ _____________ __________ _________________
1 1 false 2 0
1 1 true 8 8
1 2 false 5 0
1 2 true 5 5
2 1 false 3 0
2 1 true 7 7
2 2 false 4 0
2 2 true 5 5
3 1 true 10 10
3 2 false 6 0
3 2 true 4 4
4 1 false 6 0
4 1 true 4 4
4 2 true 10 10
5 1 true 10 10
5 2 false 5 0
5 2 true 4 4
6 1 false 1 0
6 1 true 7 7
6 2 false 5 0
6 2 true 4 4

Best Answer

Your last column is the sum of correct answer for every case and you can calculate the percentage of correct answer with that using varfun. Let's say T is your table and now you use varfun to calculate the sum of group_count for every case (true+false). So,
T_percent = varfun(@sum, T, 'GroupingVariables',{'sub_id','condition'});
now the resulting table looks like,
T_percent =
sub_id condition GroupCount sum_answer sum_group_count sum_sum_count
______ _________ __________ __________ _______________ _____________
1_1 1 1 2 1 10 8
1_2 1 2 2 1 10 5
2_1 2 1 2 1 10 7
2_2 2 2 2 1 9 5
3_1 3 1 1 1 10 10
3_2 3 2 2 1 10 4
4_1 4 1 2 1 10 4
4_2 4 2 1 1 10 10
5_1 5 1 1 1 10 10
5_2 5 2 2 1 9 4
6_1 6 1 2 1 8 7
6_2 6 2 2 1 9 4
now, you have to simply calculate the percentage of last column with it's previous column to know the percentange of right answers. So,
T_percent.Percent_True = (T_percent{:,end}./T_percent{:,end-1}).*100;
now the table looks like,
T_percent =
sub_id condition GroupCount sum_answer sum_group_count sum_sum_count Percent_True
______ _________ __________ __________ _______________ _____________ ____________
1_1 1 1 2 1 10 8 80
1_2 1 2 2 1 10 5 50
2_1 2 1 2 1 10 7 70
2_2 2 2 2 1 9 5 55.556
3_1 3 1 1 1 10 10 100
3_2 3 2 2 1 10 4 40
4_1 4 1 2 1 10 4 40
4_2 4 2 1 1 10 10 100
5_1 5 1 1 1 10 10 100
5_2 5 2 2 1 9 4 44.444
6_1 6 1 2 1 8 7 87.5
6_2 6 2 2 1 9 4 44.444
With the last column, you can create a new column for percentage_false case.