table

Syntax

table(X, [X1], [X2], …..)

or

table(capacity:size, colNames, colTypes)

Arguments

For the first scenario:

X, X1, X2 ... can be vectors, matrices or tuples. Each vector, each matrix column and each tuple element must have the same length.

For the second scenario:

capacity is a positive integer indicating the amount of memory (in terms of the number of rows) allocated to the table. When the number of rows exceeds capacity, the system will first allocate memory of 1.2~2 times of capacity, copy the data to the new memory space, and release the original memory. For large tables, these steps may use significant amount of memory.

size is an integer no less than 0 indicating the initial size (in terms of the number of rows) of the table. If size=0, create an empty table; If size>0, the initialized values are:

  • false for Boolean type;

  • 0 for numeric, temporal, IPADDR, COMPLEX, and POINT types;

  • NULL for Literal, INT128 types.

Note that if colTypes is an array vector, size must be 0.

colNames is a STRING vector of column names.

colTypes is a STRING vector of data types. It can use either the reserved words for data types or corresponding strings.

Details

For the first scenario: Converts vectors/matrices/tuples, or the combination of vectors and tuples into a table.

Note:

  • If Xk is a regular tuple, its elements must be vectors of equal length which is the number of rows of the table. Each element of the tuple is treated as a column of the table.

  • If Xk is a columnar tuple, its elements can be of unequal length, but the number of its elements must be the same as the number of rows in the table. Xk is converted into a single column (of ANY type) of the table, and each element is stored as the value of each row.

Regular tuples with elements of unequal length are stored as columnar tuples in DolphinDB. For tuples with elements of equal length, to store each element by row, you must convert them to columnar tuples with the setColumnarTuple! function.

For the second scenario: Creates an empty or initialized table of a fixed data type.

Examples

Examples of table(X, [X1], [X2], …..):

id=`XOM`GS`AAPL
x=102.1 33.4 73.6
table(id, x);
id x
XOM 102.1
GS 33.4
AAPL 73.6
table(`XOM`GS`AAPL as id, 102.1 33.4 73.6 as x);
id x
XOM 102.1
GS 33.4
AAPL 73.6

In the following example, table t is created from a vector x, a matrix y and a tuple z.

x=1..6
y=matrix(11..16, 17..22)
z=(101..106, 201..206)
t=table(x,y,z)
t.rename!(`x`y1`y2`z1`z2);

t;
x y1 y2 z1 z2
1 11 17 101 201
2 12 18 102 202
3 13 19 103 203
4 14 20 104 204
5 15 21 105 205
6 16 22 106 206

Convert a matrix into a table:

m=matrix(1 2, 3 4, 5 6);
m;
#0 #1 #2
1 3 5
2 4 6
t=table(m).rename!(`a`b`x);
t;
a b x
1 3 5
2 4 6

Currently, it is not supported to specify colTypes as ANY type using the table creation statement table(capacity:size, colNames, colTypes). If a memory table containing ANY type is needed, it must be created with an ANY vector to generate the table object. In this case, the ANY object must be a columnar tuple, meaning that the elements must be tuples of the same type. The creation method is as follows:

id = 1 2 3
val = [[1,2,3], [4,5,6],[7,8,9]]
t = table(id, val)
schema(t)
id col1 col2 col3
1 1 4 7
2 2 5 8
3 3 6 0

Examples of table(capacity:size, colNames, colTypes):

table(100:5, `name`id`value, [STRING,INT,DOUBLE]);
name id value
0 0
0 0
0 0
0 0
0 0
table(100:5, `name`id`value, `STRING`INT`DOUBLE);
name id value
0 0
0 0
0 0
0 0
0 0
table(100:1, [`value], [DOUBLE]);
value
0