Asign subjects to groups and get a group average

조회 수: 1 (최근 30일)
Johanna Popp
Johanna Popp 2022년 5월 10일
댓글: Johanna Popp 2022년 5월 10일
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

채택된 답변

Chunru
Chunru 2022년 5월 10일
% 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by