varfun
apply a function to each column of the table/timeseries
Syntax
v = varfun(f, t) v = varfun(f, t, Name, Value)
Arguments
- f
function to apply with prototype: y = f(x)
- t
table or timeseries object in input
- Name, Value
Name: 'GroupingVariables', Value: variable names or variable indices: variables values are used to create unique groups.
Name: 'InputVariables', Value: variable names or variable indices: the function will be apply only on this specified variables.
- v
table or timeseries object (same object as t).
Description
The varfun function returns a table or timeseries (same object as t) where each variable (columns) contains the result of the function applied to each variable of t.
v = varfun(f, t) applies the function f to each variable of t.
To apply the function only on certain table variables, InputVariables must be specified: v = varfun(f, t, "InputVariables", ["Var1", ...,]). It is also possible to use the syntax v = varfun(f, t(:, ["Var1", ...,])).
v = varfun(f, t, "GroupingVariables", ["Var1", ...,]) allows to specify the variables used to group the table or timeseries. The function f will be applied to each group in each variable of t. The expected result of function f must be a scalar.
Examples
v = varfun(f, t)
function y=f(x) y = x + 1; 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"]) v = varfun(f, t) // with sum v = varfun(sum, t) // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) v = varfun(f, ts) // with sum v = varfun(sum, ts)
v = varfun(f, t, "InputVariables", ["Var1", ...])
function y=f(x) y = x + 1; 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"]) v = varfun(f, t, "InputVariables", "x2") // with sum v = varfun(sum, t, "InputVariables", "x1") // Possible with extraction: t(:, ["var1", ..., "varN"]) v = varfun(f, t(:, "x2")) v = varfun(sum, t(:, "x1")) // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) v = varfun(f, ts, "InputVariables", "x2") // with sum v = varfun(sum, ts, "InputVariables", "x1") // Possible with extraction: ts(:, ["var1", ..., "varN"]) v = varfun(f, ts(:, "x2")) v = varfun(sum, ts(:, "x1"))
v = varfun(f, t, "GroupingVariables", ["Var1", ...])
function y=f(x) y = x' * x; 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"]) v = varfun(f, t, "GroupingVariables", "x") // with sum v = varfun(sum, t, "GroupingVariables", "x", "InputVariables", "x1") // With timeseries timestamp = hours([1 2 2 3 3])'; ts = timeseries(timestamp, x1, x2, "VariableNames", ["timestamp", "x1", "x2"]) v = varfun(f, ts, "GroupingVariables", "timestamp") // with sum v = varfun(sum, ts, "GroupingVariables", "timestamp", "InputVariables", "x1")
See also
- table — create a table from variables
- timeseries — create a timeseries - table with time as index
- rowfun — apply a function to each row 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.
History
バージョン | 記述 |
2024.0.0 | Introduction in Scilab. |
Report an issue | ||
<< timeseries2table | Timeseries/Table | writetable >> |