addvars
Add variables to table or timeseries
Syntax
tout = addvars(tin, var1, ..., varN) tout = addvars(tin, var1, ..., varN, "Before", loc) tout = addvars(tin, var1, ..., varN, "After", loc) tout = addvars(tin, var1, ..., varN, "NewVariableNames", newvarnames) tout = addvars(tin, var1, ..., varN, "Before", loc, "NewVariableNames", newvarnames) tout = addvars(tin, var1, ..., varN, "After", loc, "NewVariableNames", newvarnames)
Arguments
- tin
table or timeseries
- var1, ..., varN
vector or matrix of doubles, strings, booleans, duration or datetime corresponding to variables to add.
- loc
double or string value specifying the location to insert the variables based on "After" or "Before" keywords.
- newvarnames
string scalar or vector or cell containing the names of added variables. It must be used with "NewVariableNames" keyword.
- tout
table or timeseries
Description
tout = addvars(tin, var1, ..., varN) creates tout
from tin by adding the new variables var1, ..., varN at the end.
var1, ..., varN must have the same number of rows as tin.
tout = addvars(tin, var1, ..., varN, "Before", loc) creates tout
from tin by adding the new variables var1, ..., varN before loc.
loc can be double or string value. If loc is a double, then it corresponds to the nth variable
in tin. If loc is a string, then it is the name of variable in tin
tout = addvars(tin, var1, ..., varN, "After", loc) creates tout
from tin by adding the new variables var1, ..., varN after loc.
tout = addvars(..., "NewVariableNames", newvarnames) renames added variables
var1, ..., varN to newvarnames.
Examples
Add variable in table
t = table(["A"; "B"; "C"], [1; 2; 3], [4;5;6], "VariableNames", ["Key", "Value1", "Value2"]) v1 = t.Value1 + t.Value2 t2 = addvars(t, v1) v2 = t.Value2 - t.Value1 t2 = addvars(t, v1, v2)
--> t = table(["A"; "B"; "C"], [1; 2; 3], [4;5;6], "VariableNames", ["Key", "Value1", "Value2"])
t = [3x3 table]
Key Value1 Value2
___ ______ ______
A 1 4
B 2 5
C 3 6
--> v1 = t.Value1 + t.Value2
v1 = [3x1 double]
5.
7.
9.
--> t2 = addvars(t, v1)
t2 = [3x4 table]
Key Value1 Value2 Var4
___ ______ ______ ____
A 1 4 5
B 2 5 7
C 3 6 9
--> v2 = t.Value2 - t.Value1
v2 = [3x1 double]
3.
3.
3.
--> t2 = addvars(t, v1, v2)
t2 = [3x5 table]
Key Value1 Value2 Var4 Var5
___ ______ ______ ____ ____
A 1 4 5 3
B 2 5 7 3
C 3 6 9 3
Add variable in timeseries
ts = timeseries(hours(1:3)', ["A"; "B"; "C"], [1; 2; 3], [4;5;6], "VariableNames", ["Hours", "Key", "Value1", "Value2"]) v = [18;16;17]; t2 = addvars(ts, v)
--> ts = timeseries(hours(1:3)', ["A"; "B"; "C"], [1; 2; 3], [4;5;6], "VariableNames", ["Hours", "Key", "Value1", "Value2"])
ts = [3x3 timeseries]
Hours Key Value1 Value2
________ ___ ______ ______
01:00:00 A 1 4
02:00:00 B 2 5
03:00:00 C 3 6
--> v = [18;16;17];
--> t2 = addvars(ts, v)
t2 = [3x4 timeseries]
Hours Key Value1 Value2 Var4
________ ___ ______ ______ ____
01:00:00 A 1 4 18
02:00:00 B 2 5 16
03:00:00 C 3 6 17
Add variable with "After" and "Before" keywords
Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"]; NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"]; Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2 NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years t = table(Code, Area, "VariableNames", ["Code", "Area"]) // add NameContinent variable before Code t2 = addvars(t, NameContinent, "Before", "Code") // to rename t2.Var3 variable, use "NewVariableNames" option t2 = addvars(t, NameContinent, "Before", "Code", "NewVariableNames", "NameContinent") // add NumberCountry and LifeExpectancy variables after Area t3 = addvars(t2, NumberCountry, LifeExpectancy, "After", "Area", "NewVariableNames", ["NumberCountry", "LifeExpectancy"]) // can be replace by t3 = addvars(t2, NumberCountry, LifeExpectancy, "After", "Area", "NewVariableNames", {"NumberCountry", "LifeExpectancy"})
--> t = table(Code, Area, "VariableNames", ["Code", "Area"])
t = [7x2 table]
Code Area
____ ________
AF 30065000
NA 24256000
OC 7687000
AN 13209000
AS 44579000
EU 9938000
SA 17819000
--> // add NameContinent variable before Code
--> t2 = addvars(t, NameContinent, "Before", "Code")
t2 = [7x3 table]
Var3 Code Area
_____________ ____ ________
Africa AF 30065000
North America NA 24256000
Oceania OC 7687000
Antarctica AN 13209000
Asia AS 44579000
Europe EU 9938000
South America SA 17819000
--> // to rename t2.Var3 variable, use "NewVariableNames" option
--> t2 = addvars(t, NameContinent, "Before", "Code", "NewVariableNames", "NameContinent")
t2 = [7x3 table]
NameContinent Code Area
_____________ ____ ________
Africa AF 30065000
North America NA 24256000
Oceania OC 7687000
Antarctica AN 13209000
Asia AS 44579000
Europe EU 9938000
South America SA 17819000
--> // add NumberCountry and LifeExpectancy variables after Area
--> t3 = addvars(t2, NumberCountry, LifeExpectancy, "After", "Area", "NewVariableNames", ["NumberCountry", "LifeExpectancy"])
t3 = [7x5 table]
NameContinent Code Area NumberCountry LifeExpectancy
_____________ ____ ________ _____________ ______________
Africa AF 30065000 54 60
North America NA 24256000 23 78
Oceania OC 7687000 14 75
Antarctica AN 13209000 Nan Nan
Asia AS 44579000 47 72
Europe EU 9938000 45 75
South America SA 17819000 12 74
See also
- table — create a table from variables
- timeseries — create a timeseries - table with time as index
- removevars — Remove variables from table or timeseries
History
| Version | Description |
| 2025.1.0 | Function added. |
| Report an issue | ||
| << Timeseries/Table | Timeseries/Table | cell2table >> |