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
I encountered this same thing and it was frustrating as heck.
The answer/problem is that you need to "force" the data.table dcast function otherwise it will use the reshape2 function
The only way I was successfull was running dcast as follows:
# multiple value.var
data.table::dcast(dt, x + y ~ z, fun=sum, value.var=c("d1","d2"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With