1

I want to pass and use a data frame in a function that I defined. But I failed. I would like to know how to pass and use a data frame in R.

The code I used is as below:


# create example data

testData <- data.frame(man = c(9, 8, 3, 4, 8),         
                       woman = c(5, 4, 7, 1, 1),
                       love = c(1, 2, 3, 4, 5))


# define the function

polynomial <- function(iv1, iv2, dv, dataset){
  model <- lm(dv ~ iv1 + iv2 + I(iv1^2) + I(iv1 * iv2) + I(iv2^2), data = dataset)
  return(summary(model))
}

# use the function

polynomial(iv1 = man,
           iv2 = woman, 
           dv = love,
           dataset = testData)

But I got this error message - Error in eval(predvars, data, env) : object 'love' not found. Does anyone know how to solve this?

Sotos
  • 51,121
  • 6
  • 32
  • 66
wh41e
  • 183
  • 1
  • 3
  • 10

3 Answers3

5

Try the following :

polynomial <- function(iv1, iv2, dv, dataset){
  formula <- substitute(dv ~ iv1 + iv2 + I(iv1^2) + I(iv1 * iv2) + I(iv2^2))
  model <- lm(formula = formula, data = dataset)
  return(summary(model))
}

You can then use it as you wished :

polynomial(iv1 = man,
           iv2 = woman, 
           dv = love,
           dataset = testData)

substitute will replace the names dv, iv1, iv2 with the names of the arguments that you provide in your function call (in your case man, woman, love). Indeed, if you print the value of the object formula in your function you will get love ~ man + woman + I(man^2) + I(man * woman) + I(woman^2). You can also consult a related stackoverlock question or the article of H. Wickham to understand better how it works.

Etienne Kintzler
  • 672
  • 6
  • 12
  • Great ! if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. see: [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) for more information – Etienne Kintzler Nov 11 '19 at 10:31
  • I have [another question](https://stackoverflow.com/questions/58802355/how-to-center-a-variable-in-a-function-in-r), could you please help me again? – wh41e Nov 11 '19 at 13:32
1

You have to substitute the variables inside the lm call (see polynomial function below)

And when you provide the variables, it needs to be a string, other the substitute function will not work..

    polynomial <- function(iv1, iv2, dv, dataset){

    SUB=list(dv = as.name(dv),iv1=as.name(iv1),iv2=as.name(iv2))
    FORMULA = as.formula(substitute(dv ~ iv1 + iv2 + I(iv1^2) + I(iv1 * iv2) + I(iv2^2),SUB))
    model = lm(FORMULA,
      data=testData)
      return(summary(model))
    }

# use the function

polynomial(iv1 = "man",
           iv2 = "woman", 
           dv = "love",
           dataset = testData)
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
0

The function tries to find a literal object named love in the R global environment, not search the data frame for a column with name love. I recommend you read this answer to do what you want using deparse(substitute()):

https://stackoverflow.com/a/36015931/11316205

Nuclear03020704
  • 549
  • 9
  • 22