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

I use emacs-calc through an org-mode spreadsheet, and I would like to count the number of values ​​in a column that are greater than a certain value (say 10).

I am currently using emacs-calc for calculations, but if there is a solution in emacs-lisp, it would be very nice!

I know that vcount will count the number of values ​​in a vector, but it will count all the values ​​in this vector. How can I add a condition to count only values> 10?

In other words, I would like the mysterious_function to return 2 in this case:

mysterious_function([2,14,11,3,9,1]) 
+6
source share
4 answers

Tanks are all for your answers. I found a solution using emacs-calc inspired by the choroba suggestion.

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

So, for processing a column in an 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 (anonymous, in this case) to each element of the vector of the function. If the element is greater than 10, we put 1, otherwise an empty vector.

+4
source

How can I add a condition to count only values> 10?

In other words, I would like the mysterious_function to return 2 in this case:

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

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

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

  (defun mysterious-function (vector)
   (length
    (remove-if-not # '(lambda (n)
               (> = n 10))
           (append vector nil))))
+4
source

You can also create an additional column that will contain

 if($2>10,1,string("")) 

And then just apply vcount in this column.

+3
source

Since logical operations produce 1 for true and 0 for false, we can simply sum the test:

 vsum(map(<gt(#1,10)>,@ I..@II )) 
+2
source

Source: https://habr.com/ru/post/907849/


All Articles