M. Schmid
M. Schmid

Reputation: 79

mutate_at in combination with the value of a fixed column

I have a dataframe with several columns with possible target-values (in my example below value and value2) and some columns with predicted values. These columns starts all with "predMod". Now I would like to calculate the difference between all "predMod"-columns my value of the target-variable.

Example:

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

Now there should be calculated the difference between each column starting with "predMod" and the column "value2" from target.

I can reach that with:

results <- data %>%
select(date, target, contains("predMod")) %>%
mutate_at(. , vars(-c(date, target)), funs(residuals = . - value2))

results:

         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

But in my solution its hard coded in the funs ( -> funs(residuals = . - value2). If I change value2 to target ( -> funs(residuals = . - target), the whole thing does not work... How can I fix this?

One idea was to replace value2 by a position argument. The target value is always in column 2 like this. But I failed :-(

Many thanks in advance, Marco

Upvotes: 1

Views: 120

Answers (1)

akrun
akrun

Reputation: 887048

For this we need to convert the strings to symbols and do an evaluation (!!)

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

Upvotes: 1

Related Questions