Rachel Zhang
Rachel Zhang

Reputation: 564

R: mutate does not generate new column in self-write function

I would like to write a function calculating the differences between the first two columns, then take an average of them.

  errors = function(data, true, pred){
    library(dplyr)
    mutate(data, Error = data$pred - data$true)

    mean_err = mean(data$Error)
    return(mean_err)
  }

However, this function does not work as I expected. For instance, for a data source like this:

true = rnorm(10, 2, 20)
pred = rnorm(10, 1, 20)
dt = data.frame(cbind(true, pred))

This function doesn't generate a new column called "Error" and returned NA:

errors(dt, true, pred)

I was expecting the function to make the following changes to the data frame, then take an average of the errors.

mutate(dt, Error = pred-true)

enter image description here

Thank you!

Upvotes: 2

Views: 305

Answers (1)

arg0naut91
arg0naut91

Reputation: 14764

You'd do something like:

errors = function(data, true, pred) {

  require(dplyr)

  true <- enquo(true)
  pred <- enquo(pred)

  data = mutate(data, Error = !! pred - !! true)

  mean_err = mean(data$Error)

  return(mean_err)

}

As you're after a single value, this could also be shortened to:

errors = function(data, true, pred) {

  require(dplyr)

  true <- enquo(true)
  pred <- enquo(pred)

  mean_error = summarise(data, Error = mean(!! pred - !! true))

  return(mean_error)

}

Upvotes: 2

Related Questions