r - data.table way of doing this dcast -


i have data.table looks this:

tbl           lon lat hour ens       date value        1: 254  31   12   0 1994010100     0        2: 254  31   12   0 1994010200     0        3: 254  31   12   0 1994010300     0        4: 254  31   12   0 1994010400     0        5: 254  31   12   0 1994010500     0       ---                                   40494956: 269  39   24  10 2007122700   200 40494957: 269  39   24  10 2007122800   130 40494958: 269  39   24  10 2007122900   240 40494959: 269  39   24  10 2007123000   230 40494960: 269  39   24  10 2007123100   150 

and 1 looks like:

locs     lon lat  1: 260  33  2: 261  33  3: 262  33  4: 263  33  5: 260  34  6: 261  34 

and doing dcast in shape need:

temp <- dcast(tbl[locs], date ~ lon + lat + hour, fun.aggregate=mean, value.var="value") 

this want (even column names!) slow. data.table way of doing after reading few threads here, still can't quite work out. latest attempt this:

temp <- tbl[locs, list(mean = mean(value), sd = sd(value)), = list(date, lon, lat, hour)] 

to collapse ens variable (which works fine , note calculating sd well), cannot reshape it. reshape attempt is:

temp[, as.list(setattr(list(mean,sd), 'names', list(lon, lat, hour))), by=list(date)] error in setattr(list(mean, sd), "names", list(lon, lat, hour)) :    'names' attribute [3] must same length vector [2] 

any of course appreciated. thank you.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -