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
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 =
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 =
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 =
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
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.