Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Questions on gathering, grouping or clustering data based on certain criteria. This is to be used with primary tags such as list-manipulation, string-manipulation or similar tags, with the primary tag indicating the specific use case.
3
votes
Accepted
On GroupBy's *red* argument in nested groupings
GroupBy[data, {Mod[#, 5] == 0 &, Mod[#, 6] == 0 &}, Map[Total, #, {-2}]&]
<|True -> <|False -> 4605, True -> 2400|>,
False -> <|False -> 13180, True -> 2946|>|>
GroupBy[data, {Mod[#, 5] == …
6
votes
How to do a Hierarchical GroupBy?
A variation on Roman's idea:
ClearAll[hG]
hG = Normal @ GroupBy[# /. {} -> Nothing , First -> Rest, Function[x, hG[x, #2]]] /.
{Rule[a_, {b_}] :> Rule[a, b], Rule[a_, {}] :> #2[a]} &;
hG[expr …
2
votes
Accepted
GatherBy first entry of data
First /@ SplitBy[sim, Last]
{{0.0123645, 10}, {0.468865, 11}, {0.91586,12}, {0.97591, 11}, {1.01709, 10},{1.22754, 11}, {1.31259, 12}, {1.39716, 14}}
ListLinePlot[sim,
Epilog -> {Red, PointSi …
0
votes
GatherBy[list, f] except with sublists ordered by f's value
If you wish to preserve the relative ordering of elements in each group, you can do:
ClearAll[sortedGatherBy]
sortedGatherBy = SortBy[#2 @* First] @ GatherBy[#, #2] &;
Examples:
SeedRandom[1]
list = …
5
votes
Split an array with range conditions
SplitBy[sortedransam, Quotient[First[#], 2] &]
{{{0.105328, -0.0291632}, {0.253571, 0.00498561}, {0.410887, 0.171317}, {1.45579, 0.300952}},
{{2.56002, -0.0599007}, {3.67651, 0.0913857}},
{{4.44 …
0
votes
binning list of lists in unequal bin lengths
ClearAll[binF]
binF[lst_, bspec_, col_: 2] := With[{bf = #[[2]] /.
MapIndexed[Alternatives @@ # -> First[#2] &, BinLists[lst[[All, col]], bspec], 1] &},
GatherBy[lst, bf]]
Examples:
binF[list1, …
1
vote
Replace a set of vectors in a list from another set of vectors
ClearAll[f]
f[{x__}] := {x};
(f[{_, #[[2]], __}] = #) & /@ B;
f /@ A
{{0, 0.1`, 821, "new1"}, {200, 0.2`, 120, "E"}, {310, 0.32`, 911, "new"},
{400, 0.33`, 800, "GO"}}
4
votes
Accepted
Generate permutations partitioned by rotational equivalence
GroupOrbits[CyclicGroup[Length @ #], Permutations @ #, Permute] & @ list
{{{1, 1, 2, 2}, {1, 2, 2, 1}, {2, 1, 1, 2}, {2, 2, 1, 1}},
{{1, 2, 1, 2}, {2, 1, 2, 1}}}
5
votes
How to GroupBy or Gather on a nested association
Merge[Identity] @ Map[Thread[#friends -> #me] &] @ test
<|<|"gender" -> 1, "id" -> 5|> ->
{<|"a" -> 1, "id" -> 2|>, <|"a" -> 2, "id" -> 6|>},
<|"gender" -> 2, "id" -> 12|> ->
{<| …
2
votes
Accepted
How to calculate totals in datasets with GroupBy applied to Values
Using a variation of data from Alan's answer:
SeedRandom[1]
data = Dataset@
Table[Association["Sector" -> RandomChoice[{"Energy", "Env"}],
"Supplier" -> StringTemplate["N``"]@RandomInteger[5], …
4
votes
Group a list of pairs based on the path they make
For some speed-up for large input list, instead of using FindPath on all pairs of source and sink nodes in graph g, we can take pairs {v1,v2} where v1 is a source vertex in graph g and v2 is a sink no …
3
votes
Accepted
Using GatherBy with a DataSet's header designation instead of First
ds[GroupBy[#name&]] (* or *)
GroupBy[#name&][ds] (* or *)
GroupBy[ds, #name&]
1
vote
Components rules from ArrayComponents
SparseArray, like Assocoation, takes the first of repeated entries:
data = {"a", "b", "a"};
cmp = ArrayComponents[data];
Most@ArrayRules@SparseArray[cmp -> data]
{{1} -> "a", {2} -> "b"}
To ge …
6
votes
Creating lists within lists by sorting
GatherBy[SortBy[list, First], First]
GatherBy[Sort@list, First]
SplitBy[Sort@list, First]
Values @ GroupBy[First] @ Sort@list
{{{Joe, 10}}, {{John, 30}, {John, 42}}, {{Mary, 25}, {Mary, 52}}}
S …
2
votes
Accepted
Attempting to fill a table with the number of elements in each bin and make a table with the...
You can use BinLists and HistogramList as follows:
dat = {0, 1, 3, 9, 11, 13, 14, 15, 19, 20};
BinLists[dat, 5]
{{}, {0, 1, 3}, {9}, {11, 13, 14}, {15, 19}, {20}}
{binlims, bincounts} = Histo …