r - dcast 在 val.var 选项中有多个变量

标签 r

我正在使用 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/

相关文章:

r - R中的3D绘图-使用颜色的第4维

r - 带有分类变量的频率计数

r - 将数据拟合到R : errors中的分布

r - gganimate::transition_time 导致飞行多边形

r - 如何计算r中数据框中每组的平均值/中位数

r - MICE 中纵向数据的多重插补和对象类型 mids 的统计分析

r - 使用 R 从以表达式结尾的句子中提取所有单词

javascript - 使用shinyjs通过javascript在 Shiny 的应用程序中操作现有的Leaflet map

R Shiny 将图片添加到带有文本的流体行中的框中

r - R中的循环,根据不同的变量聚合数据