fixedLengthArrayVector

Syntax

fixedLengthArrayVector(args…)

Arguments

args can be vectors (excluding array vectors)/tuples/matrices/tables. All args must be of the same data type supported by array vector.

Details

Concatenate vectors, matrices, and tables and return an array vector.

Note: The length of a vector or each vector in a tuple, and the number of rows of a matrix or table must be the same.

The following figure describes how different data forms are concatenated into an array vector based on Example 1.

../../../_images/fixedLengthArrayVector.png

Examples

Example 1.

$ vec = 1 5 3
$ tp = [3 4 5, 4 5 6]
$ m =  matrix(5 0 7, 7 6 9, 1 9 0)
$ tb = table(6 9 4 as v1, 1 4 3 as v2)
$ f = fixedLengthArrayVector(vec, tp, m, tb)
$ f;
[[1,3,4,5,7,1,6,1],[5,4,5,0,6,9,9,4],[3,5,6,7,9,0,4,3]]

$ typestr(f);
FAST INT[] VECTOR

Example 2. store multiple columns as one column

$ login("admin","123456")
$ syms="A"+string(1..30)
$ datetimes=2019.01.01T00:00:00..2019.01.31T23:59:59
$ n=200
$ if(existsDatabase("dfs://stock")) {
$       dropDatabase("dfs://stock")
$ }
$ db=database("dfs://stock", RANGE, cutPoints(syms,3), engine="TSDB");
$ t=table(take(datetimes,n) as trade_time, take(syms,n) as sym,take(500+rand(10.0,n), n) as bid1, take(500+rand(20.0,n),n) as bid2)
$ t1=select trade_time, sym, fixedLengthArrayVector(bid1,bid2) as bid from t

$ quotes=db.createPartitionedTable(t1,`quotes,`sym, sortColumns=`sym`trade_time).append!(t1)
$ select * from quotes

trade_time

sym

bid

2019.01.01T00:00:00

A1

[503.111142,507.55833]

2019.01.01T00:00:30

A1

[502.991382,501.734092]

2019.01.01T00:01:00

A1

[500.790709,509.200963]

2019.01.01T00:01:30

A1

[501.127932,507.972508]

2019.01.01T00:02:00

A1

[500.678614,514.947117]

You can obtain a bid price by specifying the index. Applying a function to the bid column is equivalent to calculating on all bid prices.

$ select avg(bid[0]) as avg_bid1, avg(bid[1]) as avg_bid2, avg(bid) as avg_bid from quotes

avg_bid1

avg_bid2

avg_bid

505.0263

509.2912

507.16

Normally the field names of quotes are composed of the quote type and a number. To store multiple quote prices into an array vector, you can write the script as shown below:

// generate 50 bid/ask prices
$ n = 200
$ t=table(take(datetimes,n) as trade_time, take(syms,n) as sym)
$ for(i  in 1:51){
        t["bid"+string(i)] = take(500+rand(10.0,n), n)
$ }

// store the data into an array vector
$ t["bid"]=fixedLengthArrayVector(t["bid"+string(1..50)])
$ t1=select trade_time, sym, bid from t

To improve the performance, you can use it with function unifiedCall.

$ t["bid"]=unifiedCall(fixedLengthArrayVector, t["bid"+string(1..50)])
$ t1=select trade_time, sym, bid from t