rowfun
apply a function to each row of the table/timeseries
Syntax
r = rowfun(f, t) r = rowfun(f, t, Name, Value)
Arguments
- f
function to apply with prototype [r1, ..., rN] = f(x1, ..., xN).
- t
table or timeseries object in input
- Name, Value
Name: 'InputVariables', Value: variable names or variable indices: the function will be apply only on this specified variables.
Name: 'GroupingVariables', Value: variable names or variable indices: variables values are used to create unique groups.
Name: 'OutputVariableNames', Value: strings: variable names for output table corresponding to the wanted output arguments of the function f. If omitted, the value is generated using 'NumOutputs' ("Var1", ...).
Name: 'NumOutputs', Value: real: number of wanted output arguments of the function f. If omitted, the default value is 1 or size of 'OutputVariableNames'
- r
table or timeseries object (same object as t).
Description
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.
Examples
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)
See also
- table — create a table from variables
- timeseries — create a timeseries - table with time as index
- varfun — apply a function to each column of the table/timeseries
- groupcounts — returns the number of elements for each group
- groupsummary — create groups in table or timeseries and apply functions to variables within groups
- pivot — create a pivoted table providing a summary of data.
Report an issue | ||
<< retime | Timeseries/Table | struct2table >> |