I am currently working with the built-in data set "iris". I have calculated separate regressions for the three types of plant species like this and it worked:
Sure. Understanding is more important than implementing, in my opinion.
It's not a function. You've used expr yourself.
Here, lm(Sepal.Width~Sepal.Length) is the expr part. For details, check the documentation of with.
If you have a data frame df of two columns x and y, lm(df) performs linear regression of x on y. In iris, Sepal.Length appears first, and you wanted the opposite regression, and hence the explicit definition.
Thank you so much. It has become so much clearer to me now.
Just one last question (I promise ). It does not seem to be necessary to assign expr. It seems to work without expr just as fine (at least the output is the same).
expr it's a parameter name, if you pass parameters in the predifined order to a function then you don't necessarily have to name them, but if you want to alter the order then you need to explicitly name them, see this example.
# Order of parameters
with(data, expr, ...)
# Passing unnamed parameter in order
with(iris, { by(data.frame(Sepal.Width, Sepal.Length), Species, lm) })
# Passing named parameters in disorder
with(expr = { by(data = data.frame(Sepal.Width, Sepal.Length), INDICES = Species, FUN = lm) },
data = iris)