MATLAB Answers

Mean for certain conditions

2 views (last 30 days)
Anna
Anna on 7 Feb 2021
Edited: Anna on 10 Feb 2021
Hello,
I hope I can express my problem right, if not please ask about further details.
I am quite the beginner so I already have problems 'translating' the following basics into matlab language:
I have a table with data of a rating experiment and I need to get the mean values of certain items.
For each ID (all the '2' lines are one subject) I need a mean for the four ratings of the four phases of each snack and item. For example, I want the mean of the ratings of phases 1-4 for the item 'wanting' for the snack 'Gummibärchen', and that for each subject. ANd that for all items and snacks.
The plan after that is to visualise those differnces in heatmaps or plots but I am not sure how my desired output should look like (e.g. list or table or?). If you have tips regarding this, that would be great, too.
I have included a table with the data of 2 test IDs for testing, if that helps.
(Because the table is quite long, this screenshot only shows the beginnign of the table but as you can see the first 8 lines are all the 'Gummibärchen' ratings for phase 1 with the 8 rating-items. After that follows the same for 'Crackers', etc. Then, after the 6 snacks it switches back to 'Gummibärchen' and starts phase 2...)
Thank you for your help!

Accepted Answer

Ive J
Ive J on 7 Feb 2021
Edited: Ive J on 7 Feb 2021
t = readtable('extracted_table.xlsx');
y = groupsummary(t, {'ID', 'Item', 'Snack'}, @mean, 'Rating'); % mean of Rating for each ID, Item and Snack combination
head(y)
ID Item Snack GroupCount fun1_Rating
__ ______________ _________________ __________ ___________
2 {'Bitternis' } {'Brezeln' } 4 1.5367
2 {'Bitternis' } {'Cracker' } 4 0.44025
2 {'Bitternis' } {'Gummibaerchen'} 4 3.4337
2 {'Bitternis' } {'Kekse' } 4 0.28776
2 {'Bitternis' } {'Nic Nacs' } 4 1.8572
2 {'Bitternis' } {'Rosinen' } 4 5.322
2 {'Intensität'} {'Brezeln' } 4 52.593
2 {'Intensität'} {'Cracker' } 4 32.14
You can visualize it also with scatter3
scatter3(categorical(y.Item), categorical(y.Snack), y.fun1_Rating, 50, y.ID, 'filled')
c = colorbar(gca);
c.Ticks = unique(y.ID);
c.TickLabels = string(unique(y.ID)); % show IDs on the colorbar
  1 Comment
Anna
Anna on 7 Feb 2021
Awesome, thank you!
I totally overlooked the groupsummary function as I was always just looking at how to do it with the mean function.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!