# 3.5.2.38 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 [Book]SheetName! and two column indices colN and colM and returns a vector of row-wise sums from colN to colM.

Additionally, "row" mode can be used in Set Values and F(x)=, using a simplified syntax that is not compatible with LabTalk script.

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

## Syntax

There are two supported syntaxes:

sum(vd) // operates on a worksheet column of data

Note that sum(vd) automatically assigns values to the sum object (see below).

Alternately, ...

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

This syntax does not assign values to the sum object but does create a number of temporary datasets that store certain row-wise statistics (see below).

## Parameters

vd

a vector, can be a dataset or Origin worksheet column.

WorksheetName

the worksheet name that contains column col1 and col2.

col1, col2

the column index

## Return

For syntax

sum(vd)

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

For syntax

sum(WorksheetName, col1, col2);

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

## Examples

In this "column mode" 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
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

In the following 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)))=;

This script demonstrates "row mode" for the sum(WorksheetName, col1, col2) syntax, and how to 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;

Other "row-mode" examples for 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