我有一个数据框,其中有几列具有可能的目标值(在我的示例中为value和value2以下),有些列具有预测值。这些列均以“ predMod”开头。现在,我想计算所有“ predMod”列之间的差异,即我的目标变量的值。
示例:
target = "value2"
date value value2 predModLM1 predModLM2
1 2015-07-01 NA 5 3.236827 3.291434
2 2015-07-02 NA 5 3.236827 3.291434
3 2015-07-03 NA 5 3.236827 3.291434
4 2015-07-04 NA 5 3.236827 3.291434
5 2015-07-05 NA 5 3.236827 3.291434
6 2015-07-06 3.4 5 3.236827 3.291434
7 2015-07-07 3.4 5 3.236827 3.291434
8 2015-07-08 3.4 5 3.236827 3.291434
9 2015-07-09 3.4 5 3.236827 3.291434
10 2015-07-10 3.4 5 3.236827 3.291434
现在应该计算出以“ predMod”开头的每一列与来自目标的“ value2”列之间的差异。
我可以做到:
results <- data %>%
select(date, target, contains("predMod")) %>%
mutate_at(. , vars(-c(date, target)), funs(residuals = . - value2))
结果:
date value2 predModLM1 predModLM2 predModLM1_residuals predModLM2_residuals
1 2015-07-01 5 3.236827 3.291434 -1.763173 -1.708566
2 2015-07-02 5 3.236827 3.291434 -1.763173 -1.708566
3 2015-07-03 5 3.236827 3.291434 -1.763173 -1.708566
4 2015-07-04 5 3.236827 3.291434 -1.763173 -1.708566
5 2015-07-05 5 3.236827 3.291434 -1.763173 -1.708566
6 2015-07-06 5 3.236827 3.291434 -1.763173 -1.708566
7 2015-07-07 5 3.236827 3.291434 -1.763173 -1.708566
8 2015-07-08 5 3.236827 3.291434 -1.763173 -1.708566
9 2015-07-09 5 3.236827 3.291434 -1.763173 -1.708566
10 2015-07-10 5 3.236827 3.291434 -1.763173 -1.708566
但是在我的解决方案中,将其硬编码为funs(-> funs(residuals =。-value2)。如果我将value2更改为target(-> funs(residuals =。-target),则整个操作将不起作用。 ..我该如何解决?
一个想法是用position参数替换value2。这样,目标值始终位于第2列中。但是我失败了:-(
非常感谢, 马可
答案 0 :(得分:1)
为此,我们需要将字符串转换为符号并进行求值(!!
)
library(tidyverse)
data %>%
select(date, target, contains("predMod")) %>%
mutate_at(vars(-date, -target), funs(residuals = . - !! rlang::sym(target)))
# date value2 predModLM1 predModLM2 predModLM1_residuals predModLM2_residuals
#1 2015-07-01 5 3.236827 3.291434 -1.763173 -1.708566
#2 2015-07-02 5 3.236827 3.291434 -1.763173 -1.708566
#3 2015-07-03 5 3.236827 3.291434 -1.763173 -1.708566
#4 2015-07-04 5 3.236827 3.291434 -1.763173 -1.708566
#5 2015-07-05 5 3.236827 3.291434 -1.763173 -1.708566
#6 2015-07-06 5 3.236827 3.291434 -1.763173 -1.708566
#7 2015-07-07 5 3.236827 3.291434 -1.763173 -1.708566
#8 2015-07-08 5 3.236827 3.291434 -1.763173 -1.708566
#9 2015-07-09 5 3.236827 3.291434 -1.763173 -1.708566
#10 2015-07-10 5 3.236827 3.291434 -1.763173 -1.708566