Reputation: 193
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?
Upvotes: 1
Views: 127
Reputation: 682
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.
Upvotes: 5
Reputation: 46888
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)
Upvotes: 1
Reputation: 570
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
Upvotes: 1