avinash0513
avinash0513

Reputation: 87

dcast with multiple variables in val.var option

I am using the dcast function:

summary <- dcast(DB1, 
                 REGION_ID + REGION_NAME ~ STATUS,
                 fun.aggregate = sum, 
                 value.var = "SALES")

I am trying to use the two variables in value.var but getting an error. Below is the syntax:

summary <- dcast(DB1, 
                 REGION_ID + REGION_NAME ~ STATUS,
                 fun.aggregate = sum, 
                 value.var = c("SALES","PROFIT"))

Upvotes: 3

Views: 1276

Answers (1)

akrun
akrun

Reputation: 887511

The error occurs when we are using reshape2::dcast instead of data.table::dcast because reshape2::dcast doesn't support more than one value.var.

The documentation for ?reshape2::dcast gives

value.var - name of column which stores values, see guess_value for default strategies to figure this out.

while in ?data.table::dcast it is

value.var - Name of the column whose values will be filled to cast. Function guess() tries to, well, guess this column automatically, if none is provided. Cast multiple value.var columns simultaneously by passing their names as a character vector. See Examples.


With a small reproducible example

data(mtcars)
dcast(mtcars, vs + am ~ carb, fun.aggregate = sum, value.var = c('mpg', 'disp'))

Error in .subset2(x, i, exact = exact) : subscript out of bounds In addition: Warning messages: 1: In dcast(mtcars, vs + am ~ carb, fun.aggregate = sum, value.var = c("mpg",

If we convert to data.table

library(data.table)
dcast(as.data.table(mtcars), vs + am ~ carb, fun.aggregate = sum, value.var = c('mpg', 'disp'))
#   vs am mpg_1 mpg_2 mpg_3 mpg_4 mpg_6 mpg_8 disp_1 disp_2 disp_3 disp_4 disp_6 disp_8
#1:  0  0   0.0  68.6  48.9  63.1   0.0     0    0.0 1382.0  827.4 2082.0      0      0
#2:  0  1   0.0  26.0   0.0  57.8  19.7    15    0.0  120.3    0.0  671.0    145    301
#3:  1  0  61.0  47.2   0.0  37.0   0.0     0  603.1  287.5    0.0  335.2      0      0
#4:  1  1 116.4  82.2   0.0   0.0   0.0     0  336.8  291.8    0.0    0.0      0      0

In the OP's code, it would be

summary_out <- dcast(setDT(DB1), 
                 REGION_ID + REGION_NAME ~ STATUS,
                 fun.aggregate = sum, 
                 value.var = c("SALES","PROFIT"))

Upvotes: 1

Related Questions