Turning field values into column names in an R data frame

后端 未结 2 1121
鱼传尺愫
鱼传尺愫 2020-12-16 21:40

Hello I have the following dataframe returned using the RBloomberg library:

> tt <- bdh(conn, secs, \"last price\", \"20110501\")
> tt
          tic         


        
相关标签:
2条回答
  • 2020-12-16 22:31

    What comes to my mind is the use of tapply:

    tapply(tt$price, list(date=tt$date, ticker=tt$ticker), mean)
    

    As Mean for one element is the element itself, this should be the same. You only may get in trouble once you have more than one value for one day and ticker. (But, then, your requirement is not well defined)

    0 讨论(0)
  • 2020-12-16 22:37

    Have a look at the dcast function in package reshape2:

    library(reshape2)
    dcast(tt, date ~ ticker)
    
    
            date EURUSD USDBRL  USDINR USDTRY USDZAR
    1 2011-05-01     NA     NA      NA     NA     NA
    2 2011-05-02 1.4830 1.5893 44.3350 1.5218 6.6090
    3 2011-05-03 1.4825 1.5876 44.5150 1.5336 6.6394
    4 2011-05-04 1.4827 1.6182 44.4675 1.5471 6.6837
    5 2011-05-05 1.4539 1.6220 44.7625 1.5488 6.7250
    6 2011-05-06 1.4316 1.6149 44.7950 1.5445 6.7051
    7 2011-05-07     NA     NA      NA     NA     NA
    8 2011-05-08     NA     NA      NA     NA     NA
    
    0 讨论(0)
提交回复
热议问题