stats::calc
-- apply functions to
samplesstats::calc
(s, ..)
applies functions to
columns of the sample s
.
stats::calc(s, c, f1 <, f2, ..>)
stats::calc(s, [c1, c2, ..], f1 <, f2, ..>)
s |
- | a sample of domain type stats::sample . |
c, c1, c2, .. |
- | positive integers representing column indices of the sample. |
f1, f2, .. |
- | procedures. |
a sample of domain type stats::sample
.
stats::calc(s, c, f1)
the function
f1
is applied to the elements of the column c
of s
. This generates a new column which is appended to
s
. If present, the next function f2
is
applied to the new sample etc. Thus, a call of stats::calc
with m functions appends m new columns to
s
.
Each function must accept one parameter.
stats::calc(s, [c1, c2, ..], f1)
the
i-th element of the new column is given by f1(s[i,
c1]), s[i, c2], ..)
.
Each function must accept as many parameters as specified by the
second argument of stats::calc
.
We create a sample of three rows and three columns:
>> stats::sample([[1, a1, b1], [2, a2, b2], [3, a3, b3]])
1 a1 b1 2 a2 b2 3 a3 b3
We add and multiply the elements of the columns 2 and 3
by applying the system functions _plus
and _mult
:
>> stats::calc(%, [2, 3], _plus, _mult)
1 a1 b1 a1 + b1 a1*b1 2 a2 b2 a2 + b2 a2*b2 3 a3 b3 a3 + b3 a3*b3
The following call maps each element of the second column of the original sample to its fourth power:
>> stats::calc(%2, 2, x -> x^4)
1 a1 b1 a1^4 2 a2 b2 a2^4 3 a3 b3 a3^4
The following call computes the mean values of the rows of the last sample:
>> stats::calc(%, [1, 2, 3, 4], (x1, x2, x3, x4) -> (x1 + x2 + x3 + x4)/4)
1 a1 b1 a1^4 1/4*a1 + 1/4*b1 + 1/4*a1^4 + 1/4 2 a2 b2 a2^4 1/4*a2 + 1/4*b2 + 1/4*a2^4 + 1/2 3 a3 b3 a3^4 1/4*a3 + 1/4*b3 + 1/4*a3^4 + 3/4
The same is achieved by the following call:
>> stats::calc(%2, [1, 2, 3, 4], stats::mean)
1 a1 b1 a1^4 1/4*a1 + 1/4*b1 + 1/4*a1^4 + 1/4 2 a2 b2 a2^4 1/4*a2 + 1/4*b2 + 1/4*a2^4 + 1/2 3 a3 b3 a3^4 1/4*a3 + 1/4*b3 + 1/4*a3^4 + 3/4