I have a column with numbers and a reference column. I'm trying to separate the numbers column into first third, second third, and last third and take the average of each.
Values Ref column
1.7 cow
2.3 cow
2.6 cow
1.8 sheep
1.3 sheep
2.2 sheep
1.5 sheep
1.2 sheep
2.3 sheep
1.5 goose
2.5 goose
So, for example, the average of the first two values for "sheep", second two, and last two. In other words, I want to take the average of each 1/3 of cells adjacent to "sheep".
use a formula like this:
This does require that the ref column be sorted and like references grouped.
This array formula will return the averages even if not sorted:
array formula need to be entered with Ctrl-Shift-enter instead of Enter when exiting edit mode.
Add a column to cumulatively count the instances of the word you're looking at, then check that row number in your
AVERAGE
.=CountIf($B$2:$B2, $B2)
and fill down => values should be{1,2,3,1,2,3,4,5,6,1,2}
sheep
=CountIf($B:$B, $E$1)
=>6
{=Average(If(($B:$B = $E$1) * ($C:$C <= $E$2 / 3), $A:$A))}
(note this is an array formula, as designated by the{}
around it) =>1.55
{=Average(If(($B:$B = $E$1) * ($C:$C > $E$2 / 3) * ($C:$C <= 2 * $E$2 / 3), $A:$A))}
=>1.85
{=Average(If(($B:$B = $E$1) * ($C:$C > 2 * $E$2 / 3), $A:$A))}
=>1.75
Array formulas, if I remember correctly, are entered the same as normal formulas (don't include the
{}
, that gets entered automatically), but you press Ctrl (and possibly Shift) with Enter when you finish.NB - these look at the entire column. You can speed them up by changing
$A:$A
to$A$2:$A$12
(likewise for$B:$B
and$C:$C
). Just bear in mind that for any data you append to this list, you'll need to update the formulas; but you can insert data into the middle of the list and it will update them automatically.Well supposing there were 7 sheep values and you wanted to do a weighted mean (e.g. the first mean would be calculated from the first two sheep plus a third of the third one)?
I have attempted a general solution for this dividing any number of animals into any number of fractions and finding their average values. My approach is to use the elegant overlap formula from @Barry Houdini as used here and work out the overlap between the intervals (in the case of 7 animals divided into 3):
and the numbers of the animals
and so on.
In H4
In G4
The main formula in I4 is
entered as an array formula.
The fractions can be changed to halves, quarters etc. by changing the number in H2.