lm fits a linear model from a formula. Coefficients summarize the fitted relationship.

Program

Play the script to fit a straight line and read its slope.

linear_model.R
x <- c(1, 2, 3)
y <- c(2, 4, 6)
fit <- lm(y ~ x)
slope <- coef(fit)[["x"]]
cat(slope, "\n", sep = "")
lm `lm(y ~ x)` fits a linear model predicting `y` from `x`.
formula `y ~ x` is R formula syntax for model relationships.
coef `coef(fit)` returns fitted model coefficients.