1

Here's an example:

| low | high | students | F(students) |
|-----+------+----------+-------------|
|   0 |   30 |       15 |          15 |
|  30 |   60 |       30 |          45 |
|  60 |   70 |       40 |          85 |
|  70 |   90 |       60 |         145 |
|  90 |  100 |       15 |         160 |
#+TBLFM: @2$4..@>$4=mcol(accum(add, @2$3..@>$3), @#-1)

I hoped to do away without mcol(). For a small table like this it doesn't really matter, but, in general, this will needlessly recalculate the values as many times as there are rows. How would I avoid it?

1

This is a bad temporal solution, but it seems to be faster. There must be a better solution.

(ADDED: an important hline at the #+RESULTS output table)

#+NAME: TABLE-CUM
| students | F(students) |
|----------+-------------|
|       15 |             |
|       30 |             |
|       40 |             |
|       60 |             |
|       15 |             |
#+TBLFM: 


#+BEGIN_SRC emacs-lisp :results value :exports results :var TMP=TABLE-CUM
(require 'cl)
(setq LST (cddr TMP))                     ;; import data to list LST excluding column names and hline.
(setq VEC (apply #'mapcar* #'vector LST)) ;; transpose and change to vector VEC for function aref and aset
(setq X (first VEC))                      ;; data vector
(setq CUM (vconcat X))                    ;; temporary initial vector for cummultive sum; vconcat is required.

(dotimes (i (1- (length X)))
  (setq j (1+ i))
  (aset CUM j (+ (aref CUM (- j 1)) (aref X j))))

(setq TABLE-CUM-OUT1 (list (append X nil) (append CUM nil)))
(setq TABLE-CUM-OUT2 (apply #'mapcar* #'list TABLE-CUM-OUT1))
(append '(("students" "F(students)")) '(hline) TABLE-CUM-OUT2)
#+END_SRC

#+NAME: RESULT-TABLE-EMACS-LISP
#+RESULTS:
| students | F(students) |
|----------+-------------|
|       15 |          15 |
|       30 |          45 |
|       40 |          85 |
|       60 |         145 |
|       15 |         160 |
  • Hey, thanks for giving it a shot! Though my primary problem was to find a way to modify an existing table one column at a time (instead of modifying only one cell at a time). I also figured out that in my case there was a shorter way to write it: @2$4..@>$4=accum(add, @2$3..@>$3)_(@#-1), but I never figured how to actually splice the entire column. – wvxvw Jun 25 '15 at 15:35
  • Thank you for your kind comment, and sorry for my miss interpretation. I am very impressed with your Calc technique and programs. I did not know mcol and accum functions, they are great ! – RUserPassingBy Jun 25 '15 at 16:10

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.