Statistical Summaries
Group Average
Filtering Rows Before Summing
A grouped summary filters rows by group before it counts and sums them.
Program
Play the program to choose which group contributes to the average.
group_average.f90
Replay: real traced execution (multi-file project)
program group_average_demo
implicit none
integer :: groups(5)
integer :: scores(5)
integer :: target_group
integer :: i
integer :: count
integer :: total
integer :: average
groups = [1, 2, 1, 3, 2]
scores = [70, 80, 90, 90, 90]
target_group = 1
count = 0
total = 0
do i = 1, 5
if (groups(i) == target_group) then
count = count + 1
total = total + scores(i)
end if
end do
average = total / count
print '(I0, 1X, I0, 1X, I0)', target_group, count, average
end program group_average_demo
program group_average_demo
implicit none
integer :: groups(5)
integer :: scores(5)
integer :: target_group
integer :: i
integer :: count
integer :: total
integer :: average
groups = [1, 2, 1, 3, 2]
scores = [70, 80, 90, 90, 90]
target_group = 2
count = 0
total = 0
do i = 1, 5
if (groups(i) == target_group) then
count = count + 1
total = total + scores(i)
end if
end do
average = total / count
print '(I0, 1X, I0, 1X, I0)', target_group, count, average
end program group_average_demo
program group_average_demo
implicit none
integer :: groups(5)
integer :: scores(5)
integer :: target_group
integer :: i
integer :: count
integer :: total
integer :: average
groups = [1, 2, 1, 3, 2]
scores = [70, 80, 90, 90, 90]
target_group = 3
count = 0
total = 0
do i = 1, 5
if (groups(i) == target_group) then
count = count + 1
total = total + scores(i)
end if
end do
average = total / count
print '(I0, 1X, I0, 1X, I0)', target_group, count, average
end program group_average_demo
groups ← [1, 2, 1, 3, 2]
11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]values this step[1, 2, 1, 3, 2]groupsscores ← [70, 80, 90, 90, 90]
11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]13target_group = 1values this step[70, 80, 90, 90, 90]scorestarget_group ← 1
12scores = [70, 80, 90, 90, 90]13target_group = 114count = 0values this step1target_groupcount ← 0
13target_group = 114count = 015total = 0values this step0counttotal ← 0
14count = 015total = 016do i = 1, 5values this step0totali ← 1
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step1iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.true.groups(1) == target_groupcount ← 1
17if (groups(i) == target_group) then18 count = count + 119 total = total + scores(i)values this step0 → 1counttotal ← 70
18 count = count + 119 total = total + scores(i)20end ifvalues this step0 → 70total70scores(1)i ← 2
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step2iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(2) == target_groupi ← 3
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step3iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.true.groups(3) == target_groupcount ← 2
17if (groups(i) == target_group) then18 count = count + 119 total = total + scores(i)values this step1 → 2counttotal ← 160
18 count = count + 119 total = total + scores(i)20end ifvalues this step70 → 160total90scores(3)i ← 4
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step4iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(4) == target_groupi ← 5
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step5iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(5) == target_groupaverage ← 80
21end do22average = total / count23print '(I0, 1X, I0, 1X, I0)', target_group, count, averagevalues this step80average160total2countprint '(I0, 1X, I0, 1X, I0)', target_group, count, average
22 average = total / count23 print '(I0, 1X, I0, 1X, I0)', target_group, count, average24end program group_average_demooutput1 2 80values this step1target_group2count80average
groups ← [1, 2, 1, 3, 2]
11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]values this step[1, 2, 1, 3, 2]groupsscores ← [70, 80, 90, 90, 90]
11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]13target_group = 2values this step[70, 80, 90, 90, 90]scorestarget_group ← 2
12scores = [70, 80, 90, 90, 90]13target_group = 214count = 0values this step2target_groupcount ← 0
13target_group = 214count = 015total = 0values this step0counttotal ← 0
14count = 015total = 016do i = 1, 5values this step0totali ← 1
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step1iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(1) == target_groupi ← 2
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step2iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.true.groups(2) == target_groupcount ← 1
17if (groups(i) == target_group) then18 count = count + 119 total = total + scores(i)values this step0 → 1counttotal ← 80
18 count = count + 119 total = total + scores(i)20end ifvalues this step0 → 80total80scores(2)i ← 3
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step3iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(3) == target_groupi ← 4
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step4iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(4) == target_groupi ← 5
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step5iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.true.groups(5) == target_groupcount ← 2
17if (groups(i) == target_group) then18 count = count + 119 total = total + scores(i)values this step1 → 2counttotal ← 170
18 count = count + 119 total = total + scores(i)20end ifvalues this step80 → 170total90scores(5)average ← 85
21end do22average = total / count23print '(I0, 1X, I0, 1X, I0)', target_group, count, averagevalues this step85average170total2countprint '(I0, 1X, I0, 1X, I0)', target_group, count, average
22 average = total / count23 print '(I0, 1X, I0, 1X, I0)', target_group, count, average24end program group_average_demooutput2 2 85values this step2target_group2count85average
groups ← [1, 2, 1, 3, 2]
11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]values this step[1, 2, 1, 3, 2]groupsscores ← [70, 80, 90, 90, 90]
11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]13target_group = 3values this step[70, 80, 90, 90, 90]scorestarget_group ← 3
12scores = [70, 80, 90, 90, 90]13target_group = 314count = 0values this step3target_groupcount ← 0
13target_group = 314count = 015total = 0values this step0counttotal ← 0
14count = 015total = 016do i = 1, 5values this step0totali ← 1
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step1iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(1) == target_groupi ← 2
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step2iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(2) == target_groupi ← 3
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step3iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(3) == target_groupi ← 4
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step4iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.true.groups(4) == target_groupcount ← 1
17if (groups(i) == target_group) then18 count = count + 119 total = total + scores(i)values this step0 → 1counttotal ← 90
18 count = count + 119 total = total + scores(i)20end ifvalues this step0 → 90total90scores(4)i ← 5
15total = 016do i = 1, 517 if (groups(i) == target_group) thenvalues this step5iif (groups(i) == target_group) then
16do i = 1, 517 if (groups(i) == target_group) then18 count = count + 1values this step.false.groups(5) == target_groupaverage ← 90
21end do22average = total / count23print '(I0, 1X, I0, 1X, I0)', target_group, count, averagevalues this step90average90total1countprint '(I0, 1X, I0, 1X, I0)', target_group, count, average
22 average = total / count23 print '(I0, 1X, I0, 1X, I0)', target_group, count, average24end program group_average_demooutput3 1 90values this step3target_group1count90average
group filter
Only rows whose group matches `target_group` enter the summary.
count and total
The same branch increments the row count and adds the score.
integer average
This example keeps the average as an integer for compact output.