Skip to main content
added 334 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152

Without a Do loop

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or for speed

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}

EDIT:

Testing for a bigger set of data:

data2 =  Table[{RandomChoice[{AA, BB, CC, DD}], RandomInteger[10],RandomInteger[10]}, {10000000}];

We get:

First@Timing@Tally[data2[[All, 1]]]

2.683217

First@Timing@Tally[First@Transpose[data2]]

3.416422

First@Timing@Tally[First /@ data2]

5.304034

So mapping is the slowest as suggested by @Artes

Without a Do loop

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}

Without a Do loop

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or for speed

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}

EDIT:

Testing for a bigger set of data:

data2 =  Table[{RandomChoice[{AA, BB, CC, DD}], RandomInteger[10],RandomInteger[10]}, {10000000}];

We get:

First@Timing@Tally[data2[[All, 1]]]

2.683217

First@Timing@Tally[First@Transpose[data2]]

3.416422

First@Timing@Tally[First /@ data2]

5.304034

So mapping is the slowest as suggested by @Artes

deleted 15 characters in body
Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152

Do you needWithout a Do loop? Otherwise:

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}

Do you need a Do loop? Otherwise:

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}

Without a Do loop

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}

Source Link
rhermans
  • 37.4k
  • 4
  • 61
  • 152

Do you need a Do loop? Otherwise:

Tally[First /@ data]

{{AA, 2}, {CC, 5}, {DD, 3}}

or

Tally[data[[All, 1]]]

{{AA, 2}, {CC, 5}, {DD, 3}}