【发布时间】:2018-11-21 15:21:47
【问题描述】:
我正在处理一个需要为少数预测变量生成 MAPE 的要求。为此,我使用 MAP 函数在 data.table 中生成带有 MAPE 值的汇总表。所以结果输出应该有 4 行 x 6 列,每个城市 1 行和 1 个城市列,后跟 a1、a2、a3、a4、a5,每个单元格中都有 MAPE 值。
以下是我使用的示例数据和代码(注意 - 考虑 a1,a2,a3....作为实际和 p1,p2,p3...作为预测)-
library(data.table)
set.seed(123)
id <- seq(1001,1100,1)
city <- sample(1:4,100,replace = T)
a1 <- sample(1:100,100,replace = T)
a2 <- sample(1:100,100,replace = T)
a3 <- sample(1:100,100,replace = T)
a4 <- sample(1:100,100,replace = T)
a5 <- sample(1:100,100,replace = T)
p1 <- sample(1:100,100,replace = T)
p2 <- sample(1:100,100,replace = T)
p3 <- sample(1:100,100,replace = T)
p4 <- sample(1:100,100,replace = T)
p5 <- sample(1:100,100,replace = T)
df1 <- as.data.table(data.frame(id,city,a1,a2,a3,a4,a5,p1,p2,p3,p4,p5))
sum1 <- df1[, Map(function(x,y) mean(as.numeric(abs(get(x)-get(y))/get(x))*100),
paste("a",1:5, sep = ""),
paste("p",1:5, sep = "")),by=city]
现在我想为 x==y 然后 x > y 和 x < y..... 的行生成相同的摘要,我认为最简单的方法是将它传递给 i 但是怎么做我没有得到.....当我尝试将它作为function(x,y) get(x)==get(y) 传递时它给出了错误
i 尚未计算为逻辑、整数或双精度
请推荐
【问题讨论】:
标签: r filter data.table lapply map-function