r - can the value.var in dcast be a list or have multiple value variables? -
in files dcast.data.table
, there note stating new feature has been implemented: "dcast.data.table allows value.var column of type list"
i take mean 1 can have multiple value variables within list, i.e. in format:
dcast.data.table(dt, x1~x2, value.var=list('var1','var2','var3'))
but error: 'value.var' must character vector of length 1.
is there such feature, , if not, other one-liner alternatives?
edit: in reply comments below
there situations have multiple variables want treat value.var
. imagine example x2 consists of 3 different weeks, , have 2 value variables such salt , sugar consumption , want cast variables across different weeks. sure, can 'melt' 2 value variables single column, why using 2 functions, when can in 1 function reshape
does?
(note: i've noticed reshape
cannot treat multiple variables time variable dcast
does.)
so point don't understand why these functions don't allow flexibility include multiple variables within value.var
or time.var
allow multiple variables id.var
.
from v1.9.6 of data.table, can cast multiple value.var
columns simultaneously (and use multiple aggregation functions in fun.aggregate
). please see ?dcast
, efficient reshaping using data.tables vignette more.
here's how use dcast
:
dcast(setdt(mydf), x1 ~ x2, value.var=c("salt", "sugar")) # x1 salt_1 salt_2 salt_3 sugar_1 sugar_2 sugar_3 # 1: 1 3 4 6 1 2 2 # 2: 2 10 3 9 5 3 6 # 3: 3 10 7 7 4 6 7
Comments
Post a Comment