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.
9
votes
Merging and keeping only the sublists whose first two elements are equal
KeyValueMap[Join] @
Select[Length @ # > 1 &] @
GroupBy[list, Most -> Last]
{{-9, 3, 2, 8}, {-9, 4, 3, 4}, {-7, 2, 2, 10}, {-3, 1, 4, 6}}
3
votes
How to regroup elements of a cluster?
gatherByParent = GatherBy[#, x |-> Cases[#2, {___, x, ___}]] &;
gatherByParent[input, clustering]
{{10, 40}, {500, 200}, {6}}
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|> ->
{<| …
3
votes
Accepted
Selecting matrices from a list of matrices that are "equivalent"
Using the function sPM from this answer to generate signed permutations of a matrix with GatherBy:
sPM = Join @@ Map[Permutations @* DiagonalMatrix] @ Tuples[{-1, 1}, #] &;
grouped = GatherBy[matli …
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 …
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}}}
1
vote
Accepted
Collect terms in inverse powers of three variables
Total @ Factor @ MonomialList[Simplify @ expr, 1/{r1, r2, r3}]
To have the terms in a specific order, sort the monomials as you like and wrap the resulting list with Apply[Defer @* Plus]:
Apply[Defe …
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 = …
2
votes
Group a list based on multiples of sublists
ClearAll[proj]
proj = KroneckerProduct[#, #] &[Normalize @ Rationalize @ #] &;
GatherBy[list, proj] // Column
ClearAll[fit]
fit = Fit[{Table[0, Length@#], #}, Array[x, Length[#]-1], Array[x, Leng …
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 …
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], …
7
votes
Grouping list elements according to a condition
Select[Length @ # > 1 &] @ SequenceSplit[data, p : {{_, 0} ..., {_, 1} ..} :> p]
{{{"Call", 0}, {"Call 2", 0}, {"Response", 1}},
{{"Call 3", 0}, {"Response", 1}, {"Response 2", 1}}}
Sequence …
4
votes
How to group points based on mesh primitive membership
In version 12.1 you can use NearestMeshCells with GatherBy as follows:
grouped = GatherBy[points, NearestMeshCells[{randMesh, 2}, #] &];
Show[randMesh,
ListPlot[grouped, BaseStyle -> PointSize[Lar …
2
votes
Sorting list on multiple criteria based on means of subgroups
testdata = {{"Item 1", "Subgroup 1", "Group 1", 1}, {"Item 2",
"Subgroup 1", "Group 1", 1}, {"Item 3", "Subgroup 1", "Group 1",
1}, {"Item 4", "Subgroup 2", "Group 1", 1}, {"Item 5",
"S …
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 …