I am trying to create a function in R. I am a bit confused on how I am supposed to use the variables from the data in the function. Say I have a data frame of:
tb <- tibble(x = 1:5)
and I create a function:
f <- function(data, z){
# some function
}
I then want to be able to just do:
f(data = tb, z = x)
but I get "Error in f(data = tb, z = x): object x not found". I want to be able to do something like the lm function where I tell it the data and can then reference the variables in the function as so:
lm(mpg~cyl+disp, data = mtcars)
But I'm not sure how to make this work inside the function. Any help on how I can do this or if this makes sense?