Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally counting elements in a column in an org-mode spreadsheet

I'm using emacs-calc through org-mode spreadsheet and I would like to count the number of values, in a column, that are greater than a specific value (say 10).

I'm currently using emacs-calc for the computations, but if there is a solution in emacs-lisp, it would be very welcome!

I know that vcount would count the number of values in a vector, but that would count all the values in that vector. How could I add a condition so that only values > 10 are counted?

In other words, I would like a mysterious_function that would return 2 in such a case:

mysterious_function([2,14,11,3,9,1])
like image 550
mikael-s Avatar asked Feb 07 '12 12:02

mikael-s


2 Answers

How could I add a condition so that only values > 10 are counted?

In other words, I would like a mysterious_function that would return 2 in such a case:

mysterious_function([2,14,10,3,9,1])

Er, you only have one value greater than 10 in that list - do you mean >= 10?

Anyway, I don't know about org-mode spreadsheets, but here is how to do it in Emacs Lisp:

(defun mysterious-function (vector)
  (length
   (remove-if-not #'(lambda (n)
              (>= n 10))
          (append vector nil))))
like image 150
Luke Girvin Avatar answered Nov 15 '22 08:11

Luke Girvin


I found a solution, using emacs-calc, inspired by choroba's proposal.

vcount(map(<if(gt(#1,10), 1, [])>, [15,2,5,13]))

So, for treating a column in org-mode spreadsheet, I can do, for example:

vcount(map(<if(gt(#1,10), 1, [])>, @I..@II))

The map function is used to apply a function (an anonymous one, in that case) to each element of a vector. If the element is greater than 10, we put a 1, otherwise an empty vector.

like image 24
mikael-s Avatar answered Nov 15 '22 09:11

mikael-s