Several recent posts have remarked that it would be useful to have a general form of BinLsts which could bin a list by means of a function applied to the first level, similar in principal to SortBy. After some thinking, it occurred to me that functionality of the built-in BinLists could be transferred to a user-defined binListsBy if BinLists itself was used in the implementation.
The function BinListsBy defined below will bin the first level items in data using the values obtained when the the function f is applied to each item. binListsBy[data,f,options] accepts 2 arguments and an optional sequence of options. f is a function. When f is mapped onto the first level of data it must return a list of values acceptable to BinLists. options is an optional sequence of options which will be passed directly to BinLists. The built-in function BinLists does the real work. (An attached notebook contains the function and examples.)
Define the binListsBy function
binListsBy[data_, f_, opts___] := Module[{binBy, binning, select},
(* function f must return a list acceptable to BinLists *)
binBy = f /@ data;
(* construct bins of binBy values *)
binning = Union /@ BinLists[binBy, opts];
(* selects data elements for which f[element] is in a list *)
select[l_] := Select[data, MemberQ[l, f[#]] &];
(* use select to bin the original data according to the binning \
lists *)
select /@ binning
]
Here is an example which uses the function twice to produce a 2D binning:
Some vectors
data = RandomInteger[{0, 10}, {200, 2}];
Bin by Part 1 and then 2 with bin width 2 from 0 to 10
out = binListsBy[#, #[[2]] &, {0, 10, 2}] & /@
binListsBy[data, #[[1]] &, {0, 10, 2}];
Grid[out, Frame -> All]
Attachments: