table
create a table from variables
Syntax
t = table(var1, ..., varN) t = table(m) t = table(st) t = table(..., OptionName, Value, ...)
Arguments
- var1, ..., varN
vector or matrix with the same number of rows.
Each var can be different type: double, boolean, string, integers, datetime, duration, calendarDuration and cell.
- m
double, boolean, string, integers, datetime, duration or calendarDuration matrix
- st
structure
- "VariableNames", vector of strings
contains the name of each column of
t
. Default value: ["Var1", ..., "VarN"]- "RowNames", vector of strings
the row names for t. The names must be unique and their number must be equal to size(m, 1). Default value: [].
- t
output argument - table object.
Optional pairs OptionName, Value are:
Description
table is a data type used to store heterogeneous data. Each column of the table represents a variable, named VarN (N: column number) by default. Each variable in a table can have different data type. They must all have the same number of rows. Variable names are unique.
Regarding the extraction and insertion of data, a table is like a matrix: t(i,j) where i and j are indices. In addition to indices, it is possible to use the names of variables and rows to access the data: t.Var1 or t("Var1") where "Var1" is a variable name; t("Row1", "Var1") where "Row1" is a row name and "Var1" is a variable name. It is also possible to mix indices and variable name or row name: t(1,["Var1", "Var3"]) or t("Row1", [1 3]). The data being generally heterogeneous, it will be neccesary to use {} or table to insert data. If they are of the same type, the [] will preferred.
Use the readtable function to create a table from a file. If data contains a timestamp, use the timeseries function instead.
t = table(var1, ..., varN) creates a table from the data variables var1, ..., varN. The variables can be column vector or matrix with the same number of rows. Each var1, ..., varN can be different type.
t = table(m) creates a table from a matrix m. This syntaxe corresponds to t = matrix2table(st).
t = table(st) creates a table from a structure st. This syntaxe corresponds to t = struct2table(st).
t = table(..., OptionName, Value) can be used to specify the variable names or the row names.
'VariableNames': specify the name of variables. It is a row vector and the number of names must be equal the number of table variables. This data is stored in the properties of the table t in the 'VariableNames' field. For more information, see the Properties section below.
'RowNames': specify the name of rows. The number of names must be equal the number of table rows. This data is stored in the properties of the table t in the 'RowNames' field. For more information, see the Properties section below.
Table Properties
t contains several properties used to describe the index and the variables.
These properties can be accessed/modified by using Properties field of t.
Here is the list of available fields in t.Properties:
Description: contains the description of the table. Default value: "".
VariableNames: contains the variable names of the table. Default value: ["Var1", ..., "VarN"].
VariableDescriptions: contains the variable descriptions of the table. One description by variable. Default value: "".
VariableUnits: contains the variable units of the table. Default value: "".
RowNames: contains the row names of the table. Default value: ""
Examples
t = table(var1, ..., varN) with var1, ..., varN are column vectors
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, NameContinent, Area, NumberCountry, LifeExpectancy)
t = table(var1, ..., varN, "VariableNames", Value)
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, NameContinent, Area, NumberCountry, LifeExpectancy, ... "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) t.Properties // Add a description to the table t.Properties.Description = "table of the 7 continents" // Add variableunits to the table t.Properties.VariableUnits(3) = "km2" t.Properties.VariableUnits($) = "years" t.Properties
--> t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ... > "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) t = [7x5 table] Code NameContinent Area NumberCountry LifeExpectancy ____ _____________ ________ _____________ ______________ AF Africa 30065000 54 60 NA North America 24256000 23 78 OC Oceania 7687000 14 75 AN Antarctica 13209000 Nan Nan AS Asia 44579000 47 72 EU Europe 9938000 45 75 SA South America 17819000 12 74 --> t.Properties ans = (props mlist) with fields: Description: [] VariableNames: ['Code' 'NameContinent' 'Area' 'NumberCountry' 'LifeExpectancy'] VariableDescriptions: [] VariableUnits: [] RowNames: [] --> t.Properties.Description = "table of the 7 continents" // Add variableunits to the table --> t.Properties.VariableUnits(3) = "km2" --> t.Properties.VariableUnits($) = "years" --> t.Properties ans = (props mlist) with fields: Description: ['table of the 7 continents'] VariableNames: ['Code' 'NameContinent' 'Area' 'NumberCountry' 'LifeExpectancy'] VariableDescriptions: [] VariableUnits: ['' '' 'km2' '' 'years'] RowNames: []
t = table(var1, ..., varN, "VariableNames", Value, "RowNames", Value)
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, NameContinent, Area, NumberCountry, LifeExpectancy, ... "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) t.Row t.Row = Code t.Code = []; t(["NA", "SA"], :) t = table(NameContinent, Area, NumberCountry, LifeExpectancy, ... "VariableNames", ["NameContinent", "Area", "NumberCountry", "LifeExpectancy"], "RowNames", Code) t.Properties.RowNames t("NA", ["NameContinent", "NumberCountry"])
--> t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ... > "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) t = [7x5 table] Code NameContinent Area NumberCountry LifeExpectancy ____ _____________ ________ _____________ ______________ AF Africa 30065000 54 60 NA North America 24256000 23 78 OC Oceania 7687000 14 75 AN Antarctica 13209000 Nan Nan AS Asia 44579000 47 72 EU Europe 9938000 45 75 SA South America 17819000 12 74 --> t.Row ans = [] --> t.Row = Code t = [7x5 table] Code NameContinent Area NumberCountry LifeExpectancy ____ _____________ ________ _____________ ______________ AF AF Africa 30065000 54 60 NA NA North America 24256000 23 78 OC OC Oceania 7687000 14 75 AN AN Antarctica 13209000 Nan Nan AS AS Asia 44579000 47 72 EU EU Europe 9938000 45 75 SA SA South America 17819000 12 74 --> t.Code = []; --> t(["NA", "SA"], :) ans = [2x4 table] NameContinent Area NumberCountry LifeExpectancy _____________ ________ _____________ ______________ NA North America 24256000 23 78 SA South America 17819000 12 74 --> t = table(NameContinent, Area, NumberCountry, LifeExpectancy, ... > "VariableNames", ["NameContinent", "Area", "NumberCountry", "LifeExpectancy"], "RowNames", Code) t = [7x4 table] NameContinent Area NumberCountry LifeExpectancy _____________ ________ _____________ ______________ AF Africa 30065000 54 60 NA North America 24256000 23 78 OC Oceania 7687000 14 75 AN Antarctica 13209000 Nan Nan AS Asia 44579000 47 72 EU Europe 9938000 45 75 SA South America 17819000 12 74 --> t.Properties.RowNames ans = [7x1 string] "AF" "NA" "OC" "AN" "AS" "EU" "SA" --> t("NA", ["NameContinent", "NumberCountry"]) ans = [1x2 table] NameContinent NumberCountry _____________ _____________ NA North America 23
names = {"a"; "b"; "c"} id = [1;2;3] scores = [50; 89; 65]; t = table(names, id, scores, "VariableNames", ["names", "id", "scores"])
--> t = table(names, id, scores, "VariableNames", ["names", "id", "scores"]) t = [3x3 table] names id scores _____ ___ ______ "a" 1 50 "b" 2 89 "c" 3 65
Access to data in the table
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"]) t.x1 t(["x1", "x2"]) t([1 3 5], 2) t(6,:) = {"d", 2, -2} t.x4 = (1:6)'
--> t = table(x, x1, x2, "VariableNames", ["x", "x1", "x2"]) t = [5x3 table] x x1 x2 ___ ____ ____ a -0.5 -2.5 b 1.5 -3.5 b -1.5 -2.5 c -0.5 -3.5 a 1.5 0.5 --> t.x1 ans = [5x1 double] -0.5 1.5 -1.5 -0.5 1.5 --> t(["x1", "x2"]) ans = [5x2 double] -0.5 -2.5 1.5 -3.5 -1.5 -2.5 -0.5 -3.5 1.5 0.5 --> t([1 3 5], 2) ans = [3x1 table] x1 ____ -0.5 -1.5 1.5 --> t(6,:) = {"d", 2, -2} t = [6x3 table] x x1 x2 ___ ____ ____ a -0.5 -2.5 b 1.5 -3.5 b -1.5 -2.5 c -0.5 -3.5 a 1.5 0.5 d 2 -2 --> t.x4 = (1:6)' t = [6x4 table] x x1 x2 x4 ___ ____ ____ ___ a -0.5 -2.5 1 b 1.5 -3.5 2 b -1.5 -2.5 3 c -0.5 -3.5 4 a 1.5 0.5 5 d 2 -2 6
See also
- readtable — create a table from a file
- writetable — write a table to file
- timeseries — create a timeseries - table with time as index
History
Версия | Описание |
2024.0.0 | Introduction in Scilab. |
2026.0.0 |
|
Report an issue | ||
<< synchronize | Timeseries/Table | table2cell >> |