linear_regression <- function(variable1, variable2, dataset){
variable1 <- match(variable1, colnames(dataset))
variable2 <- match(variable2, colnames(dataset))
variable1 <- dataset[,variable1]
variable2 <- dataset[,variable2]
lm_out <- lm(variable1 ~ variable2, data = dataset)
return(summary(lm_out))
}
linear_regression('mpg', 'cyl', mtcars)
In the example above I would like to preform an operation on two variables within a dataset in a function. However in order to reference the variable in the function call I need to add the variable name in quotations and then before I can preform an operation I have to use that name to reference the column number within the dataset.
I am curious if there is a simpler way to refrence a column within a dataset within a function.