7

I want the in-built function Count to count the elements in a list which are greater than a value.

Example:

Count[{1, 1, 2, 3}, (# > 1.5) &]

Why doesn't this work?

Kuba
  • 136,707
  • 13
  • 279
  • 740
An old man in the sea.
  • 2,527
  • 1
  • 18
  • 26
  • 2
    Inverse of (21206); variant of (18054); beware of (1699). – Mr.Wizard Oct 20 '14 at 19:05
  • @Mr.Wizard Thanksfor the comment. ;) – An old man in the sea. Oct 20 '14 at 21:18
  • You're welcome. By the way I realize that the close text on this one is a bit dismissive: "simple," "easily," but that is not my intent. I could instead close this as a duplicate of one of the linked questions in the comment above if you would prefer. Also the community may weigh in and decide to reopen it. – Mr.Wizard Oct 20 '14 at 21:39
  • 2
    @Mr.Wizard I'd vote to open since the function vs. pattern issue is a common mistake for beginner/intermediate MMA users, and I've yet to see a simple Q&A that addresses the issue as this one does. – bobthechemist Oct 20 '14 at 22:05
  • @bobthechemist Did you ever cast a reopen vote? – Mr.Wizard Oct 30 '14 at 09:26
  • @Mr.Wizard Yep. the system won't let me try again (says I've already done so). Not sure if I was voted down or if there's a quirk somewhere. – bobthechemist Oct 30 '14 at 14:01
  • @bobthechemist I chose to employ moderator control to reopen this. The original close-voters will not be able to vote again, just as you were not, but if a different set of users believes this should be closed and votes accordingly I will not contravene again. – Mr.Wizard Feb 09 '16 at 08:41

2 Answers2

12

Your second argument is a function instead of a pattern.

Count[{1, 1, 2, 3}, _?(# > 1.5 &)]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Fred Simons
  • 10,181
  • 18
  • 49
  • Ups... Many thanks ;) – An old man in the sea. Oct 20 '14 at 18:26
  • @Fred Simons Dear Fred, could you please kindly comment the following. The function Select is quite close to Count by its functionality. However, it recognizes the pattern argument in the form given in the above question, that is, just #>1.5& as follows: Select[{1, 1, 2, 3}, (# > 1.5 &)] returns {2, 3}. What is the difference between these two cases? – Alexei Boulbitch Feb 09 '16 at 09:34
  • Check Mr. Wizard's comment to the OP. Specifically the first link. – Emy Feb 09 '16 at 09:38
  • @AlexeiBoulbitch please see (88220) – Mr.Wizard Feb 09 '16 at 11:56
  • Dear @Alexei. I was out for some days, so I am a bit late in replying. In the mean time you have already got the link to Mr. Wizard's answer. I can only add that for me, Select and Count are not not so close, still apart from the fact that the first one uses a criterion function and the second one a pattern. Count can be used on any level: Count[{{1,2},3,4}, _Integer, 2]. This cannot be done with Select, that works only on level 1. – Fred Simons Feb 10 '16 at 09:18
  • @ Mr.Wizard Thank you, I've got it. – Alexei Boulbitch Feb 10 '16 at 10:31
  • @ Fred Simons Thank you. That was my point that these two functions, though looking close from the first glance, are in reality different. Now I understand. – Alexei Boulbitch Feb 10 '16 at 10:32
  • @Alexei when you separate the @ symbol from the username you defeat the automatic notification. Use e.g. @FredSimons (all one word) or at least the first three letters, e.g. @Fred. The author of the post you comment under is notified automatically, so you don't actually need that here; the system may strip it if you use it in fact. – Mr.Wizard Feb 11 '16 at 09:09
5

In V10.0+ you can stick with functions:

CountsBy[{1, 1, 2, 3}, (# > 1.5) &][True]
Kuba
  • 136,707
  • 13
  • 279
  • 740