Message Boards Message Boards

0
|
3454 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

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

Posted 3 years ago

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
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.

POSTED BY: Richard Frost
Posted 3 years ago

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 BY: Rohit Namjoshi
Posted 3 years 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

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.

POSTED BY: Richard Frost
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