Asign subjects to groups and get a group average

1 Ansicht (letzte 30 Tage)
Johanna Popp
Johanna Popp am 10 Mai 2022
Kommentiert: Johanna Popp am 10 Mai 2022
Hi all,
I have a table containing 3 Variables - Subject ID (N = 1026) , Group ID (Total of 457 groups) and IQ score (one for each subject).
What I am trying to do is to
a) get a list telling me the subjects IDs belonging to each group
b) get an average IQ score for each group
Doesn't seem to be that hard of a problem but I am struggling...
Any help would be appreciated!!
Thanks,
Johanna

Akzeptierte Antwort

Chunru
Chunru am 10 Mai 2022
% 3 Variables - Subject ID (N = 1026) , Group ID (Total of 457 groups) and IQ score (one for each subject).
N = 1026;
id = randperm(N)';
gr = randi([1 457], [N, 1]);
iq = randi([80 200], [N, 1]);
data = table(id, gr, iq);
head(data)
ans = 8×3 table
id gr iq ___ ___ ___ 505 275 87 281 403 156 996 257 187 695 370 172 927 60 162 431 341 143 561 99 83 484 271 126
% a) get a list telling me the subjects IDs belonging to each group
gr0 = data.gr(1); % group to be identified
a = data(data.gr == gr0, :);
head(a)
ans = 1×3 table
id gr iq ___ ___ __ 505 275 87
% b) get an average IQ score for each group
groupsummary(data, 'gr', 'mean', 'iq')
ans = 401×3 table
gr GroupCount mean_iq __ __________ _______ 2 4 150.5 4 2 94.5 6 2 119 7 2 122 8 1 146 9 1 107 10 1 96 11 6 112 12 1 124 13 2 125.5 14 4 106.5 17 4 179.25 18 1 178 19 2 168 20 6 119.83 22 7 122.14

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by