r - Reshape2 dcast() function returning wrong values -


i have tick data equities in dow jones industrial average on course of day. here sample of data:

> head(df)                         ts  sym      ask 1: 2015-03-24 14:00:00.000 ymm5 17956.00 2: 2015-03-24 14:00:00.002 aapl   126.91 3: 2015-03-24 14:00:00.005  unh   118.35 4: 2015-03-24 14:00:00.009  xom    84.64 5: 2015-03-24 14:00:00.014  axp    81.35 6: 2015-03-24 14:00:00.016   pg    84.19 

i trying use dcast() function of reshape2 transform data wide format, like:

                        ts   aapl   axp    pg    unh   xom 1: 2015-03-24 14:00:00.000 126.91 81.35 84.19 118.35 84.64 

when try following set of commands though, here get:

tick <- data.table(read.csv("2015-3-24.csv")) df<- data.table(ts = tick$datetime, sym = tick$symbol, ask = tick$ask, bid = tick$bid) tmp <- dcast(data = df, formula = ts ~ sym)  head(tmp)                          ts aapl axp ba cat csco cvx dd dis ge gs hd ibm intc jnj jpm ko mcd mmm mrk msft nke pfe pg trv unh utx v vz wmt xom ymm5 1 2015-03-24 14:00:00.000    0   0  0   0    0   0  0   0  0  0  0   0    0   0   0  0   0   0   0    0   0   0  0   0   0   0 0  0   0   0    1 2 2015-03-24 14:00:00.002    1   0  0   0    0   0  0   0  0  0  0   0    0   0   0  0   0   0   0    0   0   0  0   0   0   0 0  0   0   0    0 3 2015-03-24 14:00:00.005    0   0  0   0    0   0  0   0  0  0  0   0    0   0   0  0   0   0   0    0   0   0  0   0   1   0 0  0   0   0    0 

i know getting formula wrong or something, no matter have though try getting same result. in advance.

several issues post, i'd clarified: (too long comment, guess @ best of now.)

  • no reproducible example.
  • you use data.table, version not shown. data.table provides efficient implementations of melt , dcast. in versions <= 1.9.4, might want use dcast.data.table. versions 1.9.5+, can directly use dcast() without loading reshape2. i'm not sure if you're using dcast reshape2 or devel version of data.table.
  • your formula (if not typo) wrong result you've shown.

you've not shown entire result dcast, believe. if complains

aggregate function missing, defaulting 'length'

then id , measure vars in formula not uniquely identify cells. minimal info provided, can guess indeed issue.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -