apply a function to each row of the table/timeseries
r = rowfun(f, t) r = rowfun(f, t, OptionName, Value, ...)
function to apply with prototype [r1, ..., rN] = f(x1, ..., xN).
table or timeseries object in input
Optional pairs OptionName, Value are:
the function will be apply only on this specified variables.
variables values are used to create unique groups.
variable names for output table corresponding to the wanted output arguments of the function f. If omitted, the value is generated using 'NumOutputs' ("Var1", ...).
number of wanted output arguments of the function f. If omitted, the default value is 1 or size of 'OutputVariableNames'.
output argument - table or timeseries object (same object as t).
The rowfun function returns a table or timeseries (same object as t) where each row contains the result of the function applied to each row of t.
r = rowfun(f, t) applies the function f to each row of t.
To apply the function only on certain table variables, InputVariables must be specified: r = rowfun(f, t, "InputVariables", ["Var1", ...,]). It is also possible to use the syntax v = rowfun(f, t(:, ["Var1", ...,])).
r = rowfun(f, t, "GroupingVariables", ["Var1", ...,]) allows to specify the variables values are used to create unique groups. The function f will be applied to each group of rows of t.
The function f can have multiple output arguments. r = rowfun(..., NumOutputs, val) allows to specify the number of output arguments. To give a name of output variable, r = rowfun(..., OutputVariableNames, ["VarOutput1", ...]) syntax must be used.
r = rowfun(f, t)
function z=f(x, y) z = x + y; endfunction rand("seed", 0) x1 = floor(rand(5,1)*5)-1.5; x2 = -floor(rand(5,1)*5)+0.5; t = table(x1, x2, "VariableNames", ["x1", "x2"]) r = rowfun(f, t) // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) r = rowfun(f, ts) | ![]() | ![]() |
r = rowfun(f, t, "InputVariables", ["Var1", ...])
function z=f(x, y) z = x + y; endfunction rand("seed", 0) x1 = floor(rand(5,1)*5)-1.5; x2 = -floor(rand(5,1)*5)+0.5; x3 = floor(rand(5,1)*5)+1; t = table(x1, x2, x3, "VariableNames", ["x1", "x2", "x3"]) r = rowfun(f, t, "InputVariables", ["x1", "x3"]) // Possible with extraction: t(:, ["var1", ..., "varN"]) r = rowfun(f, t(:, ["x1", "x3"])) // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, x3, "VariableNames", ["timestamp", "x1", "x2", "x3"]) r = rowfun(f, ts, "InputVariables", ["x1", "x3"]) r = rowfun(f, ts(:, ["x1", "x3"])) | ![]() | ![]() |
v = rowfun(f, t, "GroupingVariables", ["Var1", ...])
function z=f(x, y) z = x + y; endfunction rand("seed", 0) x = ["a"; "b"; "b"; "c"; "a"]; x1 = floor(rand(5,1)*5)-1.5; x2 = -floor(rand(5,1)*5)+0.5; x3 = floor(rand(5,1)*5)+1; t = table(x, x1, x2, "VariableNames", ["x", "x1", "x2"]) r = rowfun(f, t, "GroupingVariables", "x") t.x3 = x3; r = rowfun(f, t, "GroupingVariables", "x", "InputVariables", ["x1", "x3"]) // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) r = rowfun(f, ts, "GroupingVariables", "timestamp") | ![]() | ![]() |
v = rowfun(..., "OutputVariableNames", ["Var1", ...])
function z=f(x, y) z = x + y; endfunction rand("seed", 0) x = ["a"; "b"; "b"; "c"; "a"]; x1 = floor(rand(5,1)*5)-1.5; x2 = -floor(rand(5,1)*5)+0.5; t = table(x, x1, x2, "VariableNames", ["x", "x1", "x2"]) r = rowfun(f, t, "GroupingVariables", "x", "OutputVariableNames", "result") // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) r = rowfun(f, ts, "GroupingVariables", "timestamp", "OutputVariableNames", "result") | ![]() | ![]() |
function [z1, z2, z3]=f(x, y) z1 = x + y; z2 = x - y; z3 = x.^2 + y.^2; endfunction rand("seed", 0) x1 = floor(rand(5,1)*5)-1.5; x2 = -floor(rand(5,1)*5)+0.5; t = table(x1, x2, "VariableNames", ["x1", "x2"]) r = rowfun(f, t, "OutputVariableNames", ["a", "b", "c"]) r = rowfun(f, t, "NumOutputs", 3) // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) r = rowfun(f, t, "OutputVariableNames", ["a", "b", "c"]) r = rowfun(f, t, "NumOutputs", 3) | ![]() | ![]() |
| Version | Description |
| 2024.0.0 | Introduction in Scilab. |