Suppose I have highlighted a region of text in a buffer. The region contains numbers separated by whitespace. E.g.

1 19 23 28 41

Is there some package with ready-made interactive commands that parse the numbers from this region and do common calculations like sum and average, similar to what spreadsheet programs show in their status bar? Something like:

  • M-x sum-region => 112 shown in minibuffer
  • M-x average-region => 22.4 shown in minibuffer

or a combined command to do different calculations at once, or a modeline display.

I suppose org-mode has spreadsheet functions like this, but can they be used easily if you're not already working with an org spreadsheet? Does calc have functions that operate on region?


Found one pretty easy way using calc:

  1. Highlight the numbers in the source buffer
  2. M-x calc-grab-region (or C-x * g)
  3. M-x calc-vector-sum, M-x calc-vector-mean, M-x calc-vector-sdev, etc.

C-x * is a prefix sequence that invokes Calc in a number of ways; the letter that follows determines which one (e.g. g invokes calc-grab-region, r invokes calc-grab-rectangle).

Statistical functions can also be invoked by key sequences. For example:

  • u+: sum (calc-vector-sum)
  • uC: covariance (calc-vector-covariance)
  • uM: mean (calc-vector-mean)
  • uS: standard deviation (calc-vector-sdev)
  • vR+: sum ((calc-reduce '(2 calcFunc-add "+")))
  • vR*: product ((calc-reduce '(2 calcFunc-mul "*")))

These and other functions are covered in the Calc manual, particularly the section on Vector/Matrix Functions.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.