通过pivot函数得到的矩阵数据,转成table后,怎么得到label列

请问一下,通过pivot函数得到的矩阵数据,使用table函数转成table后,第一列的label列会丢失,有什么解决办法么?例如:

syms=`600300`600400`600500$SYMBOL
sym=syms[0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2]me=09:40:00+1 30 65 90 130 185 195 10 40 90 140 160 190 200 5 45 80 140 170 190 210
price=172.12 170.32 172.25 172. 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
volume=100 * 10 3 7 8 25 6 10 4 5 1 2 8 6 10 2 2 5 5 4 4 3
t1=table(sym, time, price, volume);
stockprice=pivot(wavg, [t1.price, t1.volume], minute(t1.time), t1.sym)
stockprice.round(2);

stockprice数据如下:

label600300600400600500
09:40m 171.7 36.28 40.15
09:41m 172.41 36.3 40.25
09:42m 175.1 36.38 40.13
09:43m 174.63 36.99 40.01
用table函数转换后,label列不见了,如下:

> table(stockprice)
col600300col600400col600500
171.7046    36.2833    40.15
172.41     36.3     40.25
175.1     36.38     40.1278
174.6312    36.9937    40.0071


请先 登录 后评论

1 个回答

wale

用rowNames()得到矩阵的label列:

t2=table(stockprice)
t2[`label]=stockprice.rowNames()

label列在最后,用reorderColumns重新排列

t2.reorderColumns!(`label join t2.colNames()[:t2.colNames().size()-1])
请先 登录 后评论