I give a simplified example in order to more clearly explain the situation.

Example: in store "A" (file A) there are 1000 watermelons, each with its own price and weight. To compile a linear regression equation, we need the result of the function lm.

In this case, we use:

a <- lm (data = A, formula = price ~ wigth) then summary (a) and voila ...

But not everything is so simple) Watermelons still have an example for an example - the country where they were grown ("Turkey" and "Spain"). So I understand that the question is very simple, but I have already raised my mind to seek a solution. How can I write a linear regression equation using the lm function only for watermelons from Turkey or only Spain. Rather, where and how to include this parameter in a <-lm (data = 'A', formula = price ~ wigth ???????)

    2 answers 2

    a <- lm(data=A[A$country=='Turkey',], formula = price~wigth) 

      The lm() function has a subset argument specifically for this.

       lm(data = mtcars, hp ~ wt, subset = cyl==4)