Sum

Description

The sum() function has two modes:

  • In "column" mode, it can take a vector and returns a vector which holds the values of the cumulative sum (from 1 to i, i=1,2,...,N). Its i+1th element is the sum of the first i elements. The last element of the returned range is the sum of all elements in the dataset.
  • In "row" mode, it takes a vector of two or more columns and returns a vector of row-wise sums.


Examples of each application are given at the end of this page.

Syntax

In "column" mode

sum(vd)

where vd is a SINGLE column.

In "column" mode, sum function automatically assigns values to the sum object (see "Return" section below).

In "row" mode

sum(vd)

where vd contains two or more columns, or part of columns. vd can be discontiguous ranges. See example 5 below.

If used in Set Values and F(x)=, following syntaxes are supported to calculate statistics quantities by row

sum(vd)_mean  //return mean
sum(vd)_median  //return median
sum(vd)_sd  //return SD
sum(vd)_min  //return minimum
sum(vd)_max  //return maximum
sum(vd)_n  //return number of numeric elements

Note that above syntaxes are only used in Set Values dialog and F(x)=, NOT compatible with LabTalk script.

In LabTalk script, this syntax is also supported:

sum(WorksheetNameorIndex, col1, col2); // operates on rows, from col1 to col2

This syntax only supports consecutive range from col1 to col2.

In "row" mode, sum function does not assign values to the sum object but does create a number of temporary datasets that store certain row-wise statistics (see "Return" section below).

Parameters

vd

in "column" mode, a vector of a dataset or Origin worksheet column,
in "row" mode, a vector of more than one dataset of column.

WorksheetName

the worksheet name or index that contains column col1 and col2. Exclamation mark ! is required as the suffix.

col1, col2

the column name or index

Return

In "column" mode

Returns a dataset whose ith element is the sum of the first i elements of the dataset vd. The sum(vd) syntax also automatically assigns values to the following Sum object properties:

  sum.mean
  sum.total
  sum.min
  sum.max
  sum.imax
  sum.imin
  sum.sd
  sum.n

In "row" mode

Returns a dataset of sums by row. Temporary datasets are also created to hold several related values generated by this syntax: _mean, _sd, _max, _min, _range, _npts.

Examples

"column" mode examples

Example 1

In this example, col(A) contains 4 values (1, 2, 3, 4). The sum() function returns the dataset (1, 3, 6, 10) where the second value in the returned range (3), is the sum of the first two values in col(A). The last value in the returned range (10) is the sum of all the values in col(A). The value of 10 is also returned in sum.total.

col(A) = {1, 2, 3, 4};
sum(col(A));
// To see the values in the returned dataset sum(col(A))
col(B) = sum(col(A));
// Column B should have values 1, 3, 6, 10

Example 2

range aa=[book2]sheet1!col(B); // assigns values in Book2, Sheet1, Col(B) to range variable aa
sum(aa); // pass aa to the sum() function
sum.mean=; // return the mean value of aa

Example 3

In this example, the user has X values in col(A) and Y values in col(B) and wants to return the X value corresponding to the max Y value.

sum(col(B)); 
col(A)[sum.imax]=;

// another solution ...
table(col(B), col(A), max(col(B)))=;

"row" mode examples

Example 4

Run following scripts in Script Window to see hwo to sum column A and B by row and return values in temporary dataset _mean.

// Prepare two columns of data in Book1 Sheet1
col(A) = {1, 2, 3, 4};
col(B) = {3, 4, 5, 6};

// Return the total by row across columns A and B and put values in column C
col(C) = sum([Book1]Sheet1!, 1, 2);

// Return the mean by row across A and B and put values in column D
col(D) = _mean;

Example 5

Following lists simple syntax examples to run in Set Values and F(x)= (cannot be used in script):

sum(A, B, D) // Set Values, F(x)= sum columns A, B, and D by row
sum(A:C, D:G, F) // Set Values, F(x)= sum columns A to C, D to G, and F by row
sum(A:D)_mean // Set Values, F(x)= calculate mean of columns A to D by row

See Also

Sum Object, Max, Mean, Median, Min, StdDev, StdDevP, Total, Table, Xindex, Xvalue