Fred,
Try:
dt = currentdatatable();
nc = ncol(dt);
nr = nrow(dt);
show(nr);show(nc);
sumcol = dt << newcolumn("sum", numeric);
for(i=1, i<=nr, i++,
sumcol[i]=0;
for(j=1, j<=nc, j++,
sumcol[i] += column(dt, j)[i];
//Show(i,j, column(dt, j)[i]);
)
);
Note 3 changes:
(1) I
interchanged the nesting of the “for” loops, putting “nc”
in the inner loop and “nr” in the outer loop.
(2) I
changed the increment variable for the outer loop to “i++” from the
constant 1; and changed the increment variable for the inner loop to “j++”
from the constant 2.
(3) For
each row, I initialized the value of sumcol to 0. Without this
initialization, each number to be added was being added to a missing value,
which yields a missing value.
Hope this helps.
Regards,
Steve Magie
From: Fred Zhang
[mailto:ieaggie2009@gmail.com]
Sent: Wednesday, February 11, 2009 7:36 AM
To: jmp-l@lists.biostat.wustl.edu
Subject: [jmp-l] How to achieve this simple Column addition in JSL?
I open a data tabel (each column is numeric type).
And want to add up each other column to a new column
"sum".
Given the following script, I always get the Log Comment:
invalid argument
in access or evaluation of 'Column' , Column( dt, j )
Can anyone help
me out?
Thanks, Fred
dt =
currentdatatable();
nc = ncol(dt);
nr = nrow(dt);
show(nr);show(nc);
sumcol = dt << newcolumn("sum", numeric);
for(i=1, i<=nc, 1,
for(j=1, j<=nr, 2,
sumcol[i] += column(dt, j)[i];
)
)
|
|