aggregate array based on condition

alosled

New Member
Joined
Mar 22, 2015
Messages
7
I am trying to get this done with a single array formula:

For row in a column array, if the array row tests positive for a condition, calculate the average of the next N rows in a different array of equal size. Each of these averages would be placed in an array, and then the average of this array would be taken.

This is the most promising thing I've been able to come up with so far:

=AVERAGE(AVERAGE(INDIRECT(IF($G$1:$G$16=2,CONCATENATE("AVERAGE(",ADDRESS(ROW($H$1:$H$16)+1,COLUMN($H$1:$H$16)),":",ADDRESS(ROW($H$1:$H$16)+2,COLUMN($H$1:$H$16)),")"),""))))

It puts the text of: average of the proper sub array I want to calculate.

But I am trying to get the actual values instead of the text. Or alternatively if there is an easier/smarter way to do this I am open to that as well.

Any help is much appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi.

Obviously it's quite difficult to work with your rather esoteric formula, since we don't have the associated context/values.

Why don't you create a very small mock dataset, e.g. 10 rows' worth, paste the necessary column entries into this thread and add your expected result? Once you have a solution for that dataset, I'm sure you'll be able to adapt it to suit your real data.

Regards
 
Upvote 0
Thank You. Apologies for formatting I am still not accustomed.

In the first column is "test" data. the second are the data values. When a row in the first column is 2, i take the average of the following two rows from the second column. I place this average in the in the third column. (since 2 shows up twice in the first column there are two values in the third column. Finally I calculate average of the values in the third column (underlined at the bottom, 5.5 is the answer I am expecting).



279
18
110
121
320
38
44
35
18
17
46
35
14
282
11
13
5.5

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Thanks.

This works for that example:

=SUM(AVERAGEIF(A1:A16,2,OFFSET(B1:B16,{1,2},)))/2

though your first post would suggest that the number of values to be considered is variable, not fixed.

Can you clarify, perhaps indicating what the solution for the dataset you provided would be if this value were to change?

Regards
 
Upvote 0
Yes I was hoping that it would be general, so that I can take the average of the values of the next N rows.
 
Upvote 0
I am looking for something generic that could be applied to the next N rows.
Yes for the case of average of the next three rows this would be a result:

2713
18
110
121
320
38
44
35
18
17
46
35
14
283
11
13
15
8

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Thanks for your help. I adapted your formula to make it sufficiently generic: by replacing {1,2} with row(indirect("1:n")).

This is good enough although I am always hesitant to use SUMIF or AVERAGEIF, type functions.

Regards.
 
Upvote 0
You're welcome, though I cannot for the life of me think why you might be hesitant about using such functions.

Regards
 
Upvote 0
well actually now I just came up with an example. I want to take the standard deviation formula also, and this formula cannot be adapted for that.
 
Upvote 0
For your average, try the following formula that needs to be confirmed with CONTROL+SHIFT+ENTER...

=AVERAGE(SUBTOTAL(1,OFFSET(B1:B16,SMALL(IF(A1:A16=2,ROW(B1:B16)-ROW(B1)+1),ROW(INDEX(A:A,1):INDEX(A:A,COUNTIF(A1:A16,2)))),0,2)))

1 - the number 1 in red means that you want an average (see the help index for other functions available for SUBTOTAL)

2 - the number 2 in blue means that you're looking for that number in Column A

2 - the number 2 in green means that you want to average the "next 2" rows

For the standard deviation, can you provide an example, as per your original post?
 
Upvote 0

Forum statistics

Threads
1,214,879
Messages
6,122,065
Members
449,064
Latest member
scottdog129

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top