Message Boards Message Boards

GROUPS:

How to find sum of a column in a dataset based on another column variable.

Posted 1 year ago
1675 Views
|
4 Replies
|
2 Total Likes
|

Type value A 12 B 8 C 10 A 2 B 3 C 10

out expected: A 14 B 11 C 20 Similar to "Counts" function. Note: I am a beginner, thanks in advance.

POSTED BY: LR LR
Answer
4 Replies

Is your data in a list of values:

{A,12,B,8,C,10,A,2,B,3,C,10}

or in 2D pairs:

{{A,12},{B,8},{C,10},{A,2},{B,3},{C,10}}

or an array:

{{A,a,12},{B,x,8},{C,q,10},{A,h,2},{B,d,3},{C,w,10}}

please clarify and provide example.

Hi LR,

List of pairs

list = {{"A", 12}, {"B", 8}, {"C", 10}, {"A", 2}, { "B", 3}, {"C", 10}}

GroupBy[list, First -> Last, Total] // Normal
(* {"A" -> 14, "B" -> 11, "C" -> 20} *)

List

list2 = {"A", 12, "B", 8, "C", 10, "A", 2, "B", 3, "C", 10}

list2 // Partition[#, 2] & // GroupBy[#, First -> Last, Total] & // Normal
Posted 1 year ago

Thank you everyone. Appreciate the replies. It is a dataset (table) and I wanted an operation similar to Excel pivot table. This is what I did, dataset[GroupBy["Key"], Total, "Values"] Any help to add multiple column values - Similar to pivot table. Thanks.

POSTED BY: LR LR
Answer

Please use the <enter code> button (third from left) to provide some example rows of your table and also the operations you are trying to use.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract