FREQUENCY_BINS

FREQUENCY_BINS(data_array,bins_array)
data_array
an array of numbers to separate into bins
bins_array
an array of numbers to represent bins

Returns data_array binned by row to match native FREQUENCY function

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Excel function FREQUENCY(data_array,bins_array) returns an array of counts of numbers in data_array based on numeric ranges determined by bins_array.

FREQUENCY_BINS filters data_array consecutively based on the bin criteria and returns a row for each bin.

Excel Formula:
=LAMBDA(data_array,bins_array,
  LET(
    pair,
      LAMBDA(pass,keep,
        LAMBDA(sw,
          SWITCH(sw,
            "passed",pass,
            "kept",keep
          )
        )
      ),
    vFlat_data,REDUCE(,data_array,vStack),
    hFlat_bins,REDUCE(,bins_array,hStack),
    sorted_bins,SORT(hFlat_bins,,1,TRUE),
    sorted_idx,
      hstack(
        SORTBY(SEQUENCE(,COLUMNS(hFlat_bins)),hFlat_bins,1),
        COLUMNS(hFlat_bins)+1),
    max_pred,isGreaterThan(MAX(hFlat_bins)),
    preds,hstack(MAP(sorted_bins,isLessThanOrEqualTo),max_pred),
    pairs,
      SCAN(pair(vFlat_data,),preds,
        LAMBDA(acc,pred,
          LET(
            vals,acc("passed"),
            passed,FILTER(vals,NOT(pred(vals)),""),
            kept,FILTER(vals,pred(vals),""),
            pair(passed,kept)
          )
        )
      ),
    binned_data,
      REDUCE(,pairs,
        LAMBDA(acc,val,
          hStack(
            IFERROR(acc("kept"),acc),
            val("kept")
          )
        )
      ),
    final,
      TRANSPOSE(SORTBY(binned_data,sorted_idx,1)),
    final
  )
)

Uses these helper functions:

isLessThanOrEqualTo
Excel Formula:
=LAMBDA(val,
  LAMBDA(vals,
    vals<=val
  )
)

isGreaterThan
Excel Formula:
=LAMBDA(val,
  LAMBDA(vals,
    vals>val
  )
)

LAMBDA_bins.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1bins_arraydata_arraybins_array
238131234-96-77.4-67.4-46.6-6.8132544.567.287
35678
4FREQUENCYCheck?frequency_bins9101112FREQUENCYCheck?frequency_bins
5331231314151611-96
6554567877-88-81-95-89-86-81-87
755910111213data_array88-77-75-71-73-69-76-72-76
8331415168087-88-288-54-55-56-67-48-60-48-51
9577384-5488-8-19-8-23-29-22-11-46
10bins_array21-8141-551010-2-1-551371313-61
11261014-143-8-567721252519211516
12-958925-197741433238433938
13FREQUENCYCheck?frequency_bins58-6768-48885758555960675048
14221296-8-89-8699808773846883878383
154434563238-77-8177899688919795100
164478910-75552519
174411121314-23882143bins_array
182215169183-60-71100806040200-20-40-60-80
19-73-5-295
20bins_array59-69-76-22FREQUENCYCheck?frequency_bins
215203010401513-4860971313878489968891839795100878383
22-1195-51674480736867
23FREQUENCYCheck?frequency_bins7-725048101057414358554359605048
245512345-761008713882125323825213938
251116-4639-968399195137131513116
2600151338-688-2-1-8-19-8-5-11-6
275567891018316-8733-23-29-22
280077-54-55-56-48-48-51-46
29551112131415###1010-67-77-75-60-71-73-69-76-72-76
300088-88-81-95-89-86-81-96-87
3100
frequency_bins
Cell Formulas
RangeFormula
D2:F2D2={3,8,13}
L2:O5L2=SEQUENCE(4,4)
T2:AC2T2=PERCENTILE.INC(L8#,SEQUENCE(,10,0,0.1))
A5:A8,A24:A30,A14:A18A5=FREQUENCY($L$2#,D2#)
B5:B8,B24:B30,R21:R31,B14:B18,R5:R15B5=BYROW(D5#,LAMBDA(row_,COUNT(row_)))
D5:H8,D24:H30,D14:G18D5=frequency_bins($L$2#,D2#)
Q5:Q15,Q21:Q31Q5=FREQUENCY($L$8#,T2#)
T5:AC15,T21:AF31T5=frequency_bins($L$8#,T2#)
L8:O27L8={80,87,-88,-2;57,73,84,-54;21,-81,41,-55;-1,43,-8,-56;-95,89,25,-19;58,-67,68,-48;96,-8,-89,-86;32,38,-77,-81;-75,55,25,19;-23,88,21,43;91,83,-60,-71;-73,-5,-29,5;59,-69,-76,-22;13,-48,60,97;-11,95,-51,67;7,-72,50,48;-76,100,87,13;-46,39,-96,83;15,13,38,-6;1,83,16,-87}
D11:G11D11={2,6,10,14}
T18:AC18T18=SEQUENCE(,10,100,-20)
D21:I21D21={5,20,30,10,40,15}
M29M29=FREQUENCY
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,575
Messages
6,173,148
Members
452,502
Latest member
PQCurious

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