Error using dcast with multiple value.var

时间:2016-02-12 19:46:57

标签: r casting data.table

So, I have been learning how to use data.table, I used the example on the ??dcast where is use the following example

dt = data.table(x=sample(5,20,TRUE), y=sample(2,20,TRUE), 
                z=sample(letters[1:2], 20,TRUE), d1 = runif(20), d2=1L) 

then

# multiple value.var
dcast(dt, x + y ~ z, fun=sum, value.var=c("d1","d2"))

and I get the error:

Error in .subset2(x, i, exact = exact) : subscript out of bounds In addition: Warning message: In if (!(value.var %in% names(data))) { :
the condition has length > 1 and only the first element will be used

here is the information of my R version:

> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          2.2                         
year           2015                        
month          08                          
day            14                          
svn rev        69053                       
language       R                           
version.string R version 3.2.2 (2015-08-14)
nickname       Fire Safety

1 个答案:

答案 0 :(得分:6)

我遇到了同样的事情,这很令人沮丧。

答案/问题是你需要“强制”data.table dcast函数,否则它将使用reshape2函数

我成功的唯一方法是运行dcast,如下所示:

# multiple value.var
data.table::dcast(dt, x + y ~ z, fun=sum, value.var=c("d1","d2"))