Skip to main content
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
Results tagged with
Search options answers only not deleted user 5478

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.

26 votes
Accepted

Partition on sublists beginning with a certain marker elements

Split[list, (#2 =!= {a}) &] { {{a}, {0, 2, 5}, {5, 4, 1}}, {{a}, {1, 1, 0}, {1, 4, 2}, {3, 3, 0}}, {{a}, {3, 2, 0}, {1, 4, 1}} } If you add Map@Rest you will get the first form. Alternative …
Kuba's user avatar
  • 138k
7 votes
Accepted

Split list when there is an empty row

Read carefully documentation of Split, you need its friend SplitBy: SplitBy[a, MemberQ[Null]] {{{1, 1, 1}, {2, 2, 2}}, {{Null, Null, Null}}, {{3, 3, 3}}}
Kuba's user avatar
  • 138k
3 votes

Replace a set of vectors in a list from another set of vectors

What about: GatherBy[ Join[A, B], #[[2]] & ][[ All , -1]] {{0, 0.1, 821, "new1"}, {200, 0.2, 120, "E"}, {310, 0.32, 911, "new"}, {400, 0.33, 800, "GO"}} If order does not matter: DeleteD …
Kuba's user avatar
  • 138k
4 votes

Taking the weighted mean of parts of a list using another list

Merge[Mean@*Apply[WeightedData]@*Transpose]@ Thread[time -> {data, weight}\[Transpose]]
Kuba's user avatar
  • 138k
7 votes

Convert association with list key to nested assocations

Merge[ KeyValueMap[ #[[1]] -> <|#[[2]] -> #2|> &, asso] , Association ] <|0 -> <|0 -> "a"|>, -1 -> <|-4 -> "b", 1 -> "c", 0 -> "d"|>, 1 -> <|0 -> "e"|>|> For deeper examples {1,2,3,4,1}-> …
Kuba's user avatar
  • 138k
3 votes
Accepted

How to merge or group an association by another association or rule?

Disclaimer: elegancy is subjective and efficiency was not tested. Merge[Total] @* KeyValueMap[mealType[#] -> #2 &] /@ <|caloriesOfFoodEaten|>
Kuba's user avatar
  • 138k
6 votes

Grouping lists by their first elements and deleting some groups

KeyValueMap[ If[Length[#2] == 1, Nothing, Join[##]] & ] @ GroupBy[data, Most -> Last] {{1, 4, 1, -3, -1}, {1, 5, 2, -4, -1}, {1, 5, 4, -3, -2}, {1, 6, 3, -5, -1}, {1, 7, 4, -6, -1}, {1, 8, …
Kuba's user avatar
  • 138k
9 votes

Creating 2 separate table from a table using a condition

n = 10^4; data = RandomReal[{0, 1}, {n, 2}]; split = GatherBy[data, #.# >= 1 &]; ListPlot[split, AspectRatio -> Automatic, Epilog -> Circle[]]
32 votes
Accepted

How to organically merge nested associations?

Initial data: peopleFacts = <| alice -> <|age -> 29, shoeSize -> 7|>, bob -> <|age -> 27, sex -> male, hair -> <|Color -> RGBColor[1, 0, 0]|> |> |>; Here is a version of RecurAssocM …
Kuba's user avatar
  • 138k
8 votes

Gather list elements by labels

I'm always afraid in case of list-manipulation that there was a duplicate in the past. But I do not remember. You can try this: Extract[list, Position[labels, #]] & /@ Union@labels {{{1 ,2}, { …
Kuba's user avatar
  • 138k
4 votes

How would you efficiently implement BinListsBy?

This probably won't be the fastest but worth to add: Last @ Reap[Scan[Sow[#, Floor[#[[ 1]]/10]] &, data], Range[2, 7]] {{ {{21, "Alice"}, {27, "Bob"}, {22, "Charles"}}}, {{{34, "D …
Kuba's user avatar
  • 138k
6 votes

Grouping words by their differences

words = {"horse", "horses", "morse", "morses", "norse", "norses", "fox", "monses", "goose", "tool", "goal", "tools", "gothe"} I'm not very experienced with Graphs so any advice, how to make it sh …
Kuba's user avatar
  • 138k
3 votes
Accepted

Table from grouping an association by value

GroupBy[a, Identity, Keys] // Dataset
4 votes

Merge associations always taking the value that satisfies a given test

Merge[data, First @* MaximalBy[First]] <|a -> {3, l2}, b -> {3, l1}|>
Kuba's user avatar
  • 138k
4 votes
Accepted

Formatting plot of symmetric points differently to asymmetric points

list = DeleteDuplicates@RandomInteger[{-10, 10}, {155, 2}]; axis = 1/2; First let's gather points with the same distance form your axis and the same second coordinate. Those which have simmetric p …
Kuba's user avatar
  • 138k

15 30 50 per page