Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

对下面股票行情表t1进行计算每分钟的交易量的加权平均值得到stockprice:

syms=`BIDU`MSFT`ORCL$SYMBOL
sym=syms[0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2]
price=172.12 170.32 172.25 172.55 175.1 174.85 174.5 36.45 36.15 36.3 35.9 36.5 37.15 36.9 40.1 40.2 40.25 40.15 40.1 40.05 39.95
qty=100* 10 3 7 8 25 6 10 4 5 1 2 8 6 10 2 2 5 5 4 4 3
trade_time=09:40:00+1 30 65 90 130 185 195 10 40 90 140 160 190 200 5 45 80 140 170 190 210
t1=table(sym, price, qty, trade_time);
stockprice=pivot(wavg, [t1.price, t1.qty], minute(t1.trade_time), t1.sym).round(2)

image.png

用typestr(stockprice.columnNames()[0]) 显示列的类型是string,
但按列名访问:

stockprice[`BIDI]

报错"Incompatible type. Expected: INT, Actual: STRING",如下图所示:
image.png

请问这是为什么?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

pivot函数返回的是一个矩阵。矩阵不能直接通过列的label来访问,必须通过下标来访问。

stockprice[`BIDI]

改成

stockprice[1]

可以解决问题。如果不想直接用下表,可以将代码改成

stockprice[stockPrice.columnNames().find(`BIDI)]

另外,如果想生成一个table而不是matrix,可以使用SQL:

stockprice = select wavg(price, qty).round(2) from t1 pivot by minute(trade_time) as trade_time, sym

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...