我正在使用 dcast
函数:
summary <- dcast(DB1,
REGION_ID + REGION_NAME ~ STATUS,
fun.aggregate = sum,
value.var = "SALES")
我正在尝试使用 value.var 中的两个变量,但出现错误。以下是语法:
summary <- dcast(DB1,
REGION_ID + REGION_NAME ~ STATUS,
fun.aggregate = sum,
value.var = c("SALES","PROFIT"))
最佳答案
当我们使用 reshape2::dcast
而不是 data.table::dcast
时会发生错误,因为 reshape2::dcast
没有不支持多个 value.var
。
?reshape2::dcast
的文档给出了
value.var - name of column which stores values, see guess_value for default strategies to figure this out.
在 ?data.table::dcast
中是
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.
用一个小的可重现的例子
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",
如果我们转换为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
在 OP 的代码中,它将是
summary_out <- dcast(setDT(DB1),
REGION_ID + REGION_NAME ~ STATUS,
fun.aggregate = sum,
value.var = c("SALES","PROFIT"))
关于r - dcast 在 val.var 选项中有多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61130613/